【问题标题】:Exposing property of a control inside a WPF UserControl在 WPF UserControl 中公开控件的属性
【发布时间】:2012-10-08 23:49:58
【问题描述】:

我有一个用户控件,里面有一个网格控件

 <UserControl x:Class="MyGrid">
      <Telerik:RadGridView EnableRowVirtualization="false"> 
     </Telerik:RadGridView/>
 </UserControl>

如何使用 DependencyProperty 在用户控件中公开控件的 EnableRowVirtualization 属性,以便当有人使用 MyGrid 用户控件时,用户将执行类似的操作

  <grids:MyGrid  EnableRowVirtualization="false"> </grids:MyGrid>

更新:现在,这正是我想出的

 public partial class MyGrid //myGrid userControl
 {
    public bool EnableRowVirtualization
    {
        get { return (bool)GetValue(EnableRowVirtualizationProperty); }
        set { SetValue(EnableRowVirtualizationProperty, value); }
    }

    // Using a DependencyProperty as the backing store for EnableRowVirtualization.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnableRowVirtualizationProperty =
        DependencyProperty.Register("EnableRowVirtualization", typeof(bool), typeof(MaxGridView), new UIPropertyMetadata(false, OnEnableRowVirtualizationPropertyChanged)
     );


    private static void OnEnableRowVirtualizationPropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        var grid = (RadGridView)depObj;

        if (grid != null)
        {
            grid.EnableRowVirtualization = (bool)e.NewValue;
        }
    }

【问题讨论】:

    标签: wpf dependency-properties


    【解决方案1】:

    如果您给 Telerik 网格命名,您就可以从依赖属性的代码中访问它。如果您在定义依赖项属性时还将它与 PropertyChanged 属性元数据结合起来,那么您可以简单地将值传递到底层网格。

    这只是我的想法,但这样的事情应该可以解决问题:

    public static readonly DependencyProperty EnableRowVirtualizationProperty =
        DependencyProperty.Register("EnableRowVirtualization"
        , typeof(bool)
        , typeof(MyGrid)
        , new UIPropertyMetadata(false, OnEnableRowVirtualizationPropertyChanged) 
        );
    
    
    private static void OnEnableRowVirtualizationPropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        var myGrid = depObj as MyGrid;
        if (myGrid != null)
        {
            myGrid.InnerTelerikGrid.EnableRowVirtualization = e.NewValue;
        }
    }
    

    如需了解更多信息,请查看DependencyProperty.RegisterAttached Method (String, Type, Type, PropertyMetadata)UIPropertyMetadata Constructor (Object, PropertyChangedCallback)

    【讨论】:

    • 谢谢,你能检查我上面的编辑吗?似乎 OnEnableRowVirtualizationPropertyChanged 事件从未触发,我也尝试使用 PropertyMetada 但无济于事..
    • 没关系,让它工作,我只需要访问 OnEnableRowVirtualization var myGrid= (MyGrid)depObj; var grid = myGrid.RadGridView;做这个把戏
    猜你喜欢
    • 2015-04-01
    • 2015-07-26
    • 2017-07-07
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2010-09-23
    • 2011-12-26
    • 2020-04-11
    相关资源
    最近更新 更多