2

内部にグリッドコントロールがあるユーザーコントロールがあります

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

DependencyPropertyを使用してusercontrol内のコントロールのEnableRowVirtualizationプロパティを公開して、誰かがMyGrid usercontrolを使用したときに、ユーザーが次のようなことを行うようにするにはどうすればよいですか?

  <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 に答える