2016-05-24 10 views
0

2つのカスタム依存関係プロパティSelectedItem、ItemsSourceを持つコントロールを作成します(listViewExにしましょう)。 このコントロールはContentControlから派生し、ContentプロパティをListViewインスタンスに設定します。なぜバインディングが予測できないのですか?

listViewExは、次のバインディングしている:

listViewEx.SelectedItem - > ListView.SelectedItem、 listViewEx.ItemsSource - > ListView.ItemsSource

は、今私は(2つのコンボボックスで、フォームを得ました> SelectedPerson、-

box1.SelectedItem:BOX1とBOX2)と1 listViewExは、 フォームは、次のバインディングを持っていますbox1.ItemsSource - >人

box2.SelectedItem - > SelectedPerson、 box2.ItemsSource - >人

listViewEx.SelectedItem - > SelectedPerson、 listViewEx.ItemsSource - >人

形対応するSelectedPersonとPersonsのプロパティと値を持つ有効なDataContext値を持ちます。

今の質問:なぜ、すべてのバインディングはこの1つを除いて正常に動作します:

listViewEx.SelectedItem - 私は、コンボボックスの値を変更した場合> SelectedPerson

- 私までlistViewExにchaged選択よりそのコンボボックスが正常に動作した後、ListViewExで選択を変更しますが、listViewExはすべてのバインディングを失います。私は間違っているの?

Problem reproduction video (gif)

listViewExコード:

public class ListViewEx : ContentControl 
{ 
    ListView list = new ListView(); 

    public IEnumerable ItemsSource 
    { 
     get { return (IEnumerable)GetValue(ItemsSourceProperty); } 
     set { SetValue(ItemsSourceProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ItemsSourceProperty = 
     DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ListViewEx), new UIPropertyMetadata(null)); 


    public object SelectedItem 
    { 
     get { return (object)GetValue(SelectedItemProperty); } 
     set { SetValue(SelectedItemProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for SelectedItem. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty SelectedItemProperty = 
     DependencyProperty.Register("SelectedItem", typeof(object), typeof(ListViewEx), new UIPropertyMetadata(null)); 

    public ListViewEx() 
    { 
     Content = list; 
     list.SetBinding(ListView.SelectedItemProperty, "SelectedItem"); 
     list.SetBinding(ListView.ItemsSourceProperty, "ItemsSource"); 
     list.DataContext = this; 
    } 
} 

フォームコード:

public partial class MainWindow : Window 
{ 
    Data data; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     data = new Data 
     { 
      Persons = new[] { 
       new Person{ Age=11, Name="Ivan"}, 
       new Person{ Age=10, Name="Petr"}, 
       new Person{ Age=20, Name="Masha"}, 
       new Person{ Age=30, Name="Dasha"}, 
       new Person{ Age=40, Name="Gennadiy"}, 
       new Person{ Age=50, Name="Viktor"}, 
       new Person{ Age=90, Name="Victory!"}, 
      } 
     }; 

     DataContext = data; 
    } 

    class Data 
    { 
     public Person[] Persons { get; set; } 
     public object SelectedPerson { get; set; } 
    } 

    class Person 
    { 
     public int Age { get; set; } 
     public string Name { get; set; } 
     public override string ToString() 
     { 
      return Name; 
     } 
    } 
} 

フォームXAML:

<Window x:Class="DependencyPropertyTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:DependencyPropertyTest" 
    Title="MainWindow" Height="350" Width="525"> 
<StackPanel> 
    <ComboBox ItemsSource="{Binding Persons}" SelectedItem="{Binding SelectedPerson}"></ComboBox> 
    <ComboBox ItemsSource="{Binding Persons}" SelectedItem="{Binding SelectedPerson}"></ComboBox> 
    <local:ListViewEx ItemsSource="{Binding Persons}" SelectedItem="{Binding SelectedPerson}" Height="400"></local:ListViewEx> 
</StackPanel> 

答えて

2

デフォルトで双方向の結合としてあなたListViewExコントロールのSelectedItemプロパティは、双方向、どちらか

<local:ListViewEx SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" ... /> 

を書き込むことによって、またはそれを宣言することによって拘束されるべきである。

public static readonly DependencyProperty SelectedItemProperty = 
    DependencyProperty.Register(
     "SelectedItem", 
     typeof(object), 
     typeof(ListViewEx), 
     new FrameworkPropertyMetadata(
      FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 
+0

ありがとうございました!あなたのソリューションは機能します!それは私のために多くの時間を殺したので、愚かな間違いです。 –

関連する問題