1

グリッドを含むカスタム UserControl がありますItemsSource...リソース ディクショナリのデータ テンプレートの xaml コードによってそのグリッドのプロパティを設定したい...次に、依存関係プロパティを使用しました...これが私の実装です。 ..

public partial class MyControlGrid : UserControl
{

    // Dependency Property
    public static readonly DependencyProperty MyItemSourceProperty =
         DependencyProperty.Register("MyItemSource", typeof(ICollectionView),
         typeof(MyControlGrid), new FrameworkPropertyMetadata(null, OnMyItemSourcePropertyChanged));      

    IDictionary<string, string> _columns = new Dictionary<string, string>();


    private static void OnMyItemSourcePropertyChanged(DependencyObject obj, 
        DependencyPropertyChangedEventArgs args)
    {
        // When the color changes, set the icon color PlayButton
        MyControlGrid muc = (MyControlGrid)obj;
        ICollectionView value = (ICollectionView)args.NewValue;
        if (value != null)
        {
            muc.MyGridControl.ItemsSource = value;
        }
    }

    public ICollectionView MyItemSource
    {
        get
        {
            return (ICollectionView)GetValue(MyItemSourceProperty);
        }
        set
        {
            SetValue(MyItemSourceProperty, value);
            //OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value));
            // Old value irrelevant.
        }
    }


    public MyControlGrid()
    {
        InitializeComponent();


    }
}

これはユーザー コントロールの xaml コードです

<UserControl x:Class="GUI.Design.Templates.MyControlGrid"
    Name="MyListControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfTkit="http://schemas.microsoft.com/wpf/2008/toolkit"
             xmlns:Templates="clr-namespace:Emule.GUI.Design.Templates">

<StackPanel>

        <WpfTkit:DataGrid   ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Templates:MyControlGrid}}, Path=MyItemSource}"
                            x:Name="MyGridControl"  

<StackPanel>

これは私が使用するバインディング パス式です

<basic:MyControlGrid MyItemSource="{Binding MyDataContextVisibleCollection}"/>

これは機能せず、wpf 出力ウィンドウにエラーが表示されません

当然、これをユーザーコントロールに直接バインドすると正常に動作することに注意してください

<WpfTkit:DataGrid   ItemsSource="{Binding MyDataContextVisibleCollection}"

私が間違っていますか?

ありがとう

ps私の英語でごめんなさい

4

1 に答える 1

1

この 答えは私に道を示します

PropertyChangedCallback私のコードで問題なく使用できます:

public static readonly DependencyProperty MyItemSourceProperty =
              DependencyProperty.Register("MyItemSource", typeof(IEnumerable),
              typeof(MyControlGrid), new FrameworkPropertyMetadata(null,
              new PropertyChangedCallback(MyControlGrid.OnItemsSourceChanged)));

または、コメントを削除しOnTargetPowerChangedてプロパティ変更イベントを発生させる必要があります

set
{
    SetValue(MyItemSourceProperty, value);
    //OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value));
    // Old value irrelevant.
}

で修正

public ICollectionView MyItemSource
{
    get
    {
        return (ICollectionView)GetValue(MyItemSourceProperty);
    }
    set
    {
        SetValue(MyItemSourceProperty, value);
        OnItemsSourceChanged(this, new DependencyPropertyChangedEventArgs(MyItemSourceProperty, value, value));
    }
}
于 2012-05-16T07:23:28.337 に答える