2

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

 <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;
        }
    }
4

1 回答 1

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)

于 2012-10-09T00:19:49.163 回答