【问题标题】:Binded Grid´s Width - but didn't refresh绑定网格的宽度 - 但没有刷新
【发布时间】:2013-09-15 07:01:39
【问题描述】:

我在 WPF 中绑定了一个 Grid 的宽度:

<Grid Width="{Binding MDIWidth, RelativeSource={RelativeSource AncestorType=dx:DXWindow}}" Name="GridControl">  
...
</Grid>

属性已更改,但如果属性已更改,则 Grid 未刷新其大小。如何做到这一点?

【问题讨论】:

    标签: wpf xaml binding grid width


    【解决方案1】:

    我只能建议您的Binding 无法工作,因为您可以使用Binding 设置Grid.Width 属性。如果您使用RelativeSource Binding 来访问UserControl 后面的代码,其中Grid 是,那么在构造函数中尝试这个作为测试...:

    public DXWindow()
    {
        InitializeComponent();
        DataContext = this; 
        MDIWidth = 100.0;
    }
    

    然后在视图中:

    <Grid Width="{Binding MDIWidth}" Name="GridControl" />
    

    如果这可行,那么您原来的Binding 有错误。

    更新>>>

    如果您使用UserControl 后面的代码来声明您的属性,那么您需要声明DependencyProperty:

    public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(
        "Width", typeof(double), typeof(MainWindow), new UIPropertyMetadata(250.0));
    
    public double Width
    {
        get { return (double)GetValue(WidthProperty); }
        set { SetValue(WidthProperty, value); }
    }
    

    除了你可能不使用DependencyProperty,我不知道为什么你的代码不起作用......你当然可以随时使用@987654336 更新Grid 的Width @。

    【讨论】:

    • 我认为你是对的(除非你打错字),你不能使用绑定来设置 Grid.Width 属性,因为它不是依赖属性。
    • 我没有犯任何错误...我什至测试过...也许你应该在评论之前尝试过?
    • 评论中的错字,而不是代码中的错字。 “你可以设置”->“你不能设置”。
    • 反正我错了,Width是一个依赖属性,你可以绑定到它。
    • 您好,绑定不是我的问题。我在第一篇文章中的绑定对我有用。它与属性值绑定。我的问题是,如果在运行时更改了属性的值,则网格没有刷新/调整大小。
    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2011-06-24
    • 2017-06-11
    • 2020-05-14
    • 1970-01-01
    • 2017-02-21
    相关资源
    最近更新 更多