【问题标题】:Why can't I bind the margin property?为什么我不能绑定 margin 属性?
【发布时间】:2013-09-09 14:14:13
【问题描述】:

我有一块矩形板,我想动态缩放。我可以设置矩形元素的高度和宽度属性。唯一不合作的属性是保证金..

我尝试将 ViewModel 中的相同属性边距绑定到宽度、高度和边距,它只适用于宽度和高度。一旦我在矩形的边距属性上尝试它,加载窗口需要很长时间,并且它最终显示为没有边距..

有人知道为什么会这样吗?

矩形:

<Rectangle Margin="{Binding ElementName=root, Path=DataContext.Margin}" Fill="White" Height="{Binding ElementName=root, Path=DataContext.Margin}" Width="{Binding ElementName=root, Path=DataContext.Margin}"></Rectangle>

属性:

private int _margin = 5;
    public int Margin
    {
        get
        {
            return _margin;
        }
    }

【问题讨论】:

  • 一个快速简单的解决方法是现在只使用Border 而不是Rectangle。我会在以后有时间的时候尝试重新创建你得到的东西,因为这很奇怪。

标签: c# xaml data-binding properties margin


【解决方案1】:

好的,我修好了! 使用 int 或 double 作为边距似乎是一个问题。边距是厚度类型!无论如何感谢所有的帮助!

private Thickness _vakMargin;
public Thickness VakMargin
{
    get
    {
        return _vakMargin;
    }
    set
    {
        _vakMargin = value;
    }
}

我确实为所有 4 个边距设置了:

new Thickness(someDouble);

【讨论】:

    【解决方案2】:

    我认为您的问题可能与您绑定的方式有关,因为我无法重现该问题。也许您可以发布更多代码来确定确切原因。但是,以下内容对我有用:

    XAML:

    <Window.Resources>
        <local:MyRectangle x:Key="myRectangle" />
    </Window.Resources>
    
    <Grid DataContext="{StaticResource myRectangle}">
            <Rectangle Width="{Binding Path=MyWidth}" Height="{Binding Path=MyHeight}" Margin="{Binding Path=MyMargin}"  />
    </Grid>
    

    类代码:

    public class MyRectangle
    {
        public double MyMargin { get; set; }
        public double MyWidth {get; set;}
        public double MyHeight {get; set;}
    
        public MyRectangle(double dHeight, double dWidth, double dMargin)
        {
            MyHeight = dHeight;
            MyWidth = dWidth;
            MyMargin = dMargin;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      相关资源
      最近更新 更多