Ich erstelle ein Steuerelement (lass es listViewEx sein) mit zwei benutzerdefinierten Abhängigkeitseigenschaften: SelectedItem, ItemsSource. Dieses Steuerelement wird von ContentControl abgeleitet und die Content-Eigenschaft auf die ListView-Instanz festgelegt.Warum Binding funktioniert unvorhersehbar?
listViewEx haben neben Bindungen:
listViewEx.SelectedItem -> ListView.SelectedItem, listViewEx.ItemsSource -> ListView.ItemsSource
jetzt ich habe eine Form, mit zwei Kombinationsfelder (box1 und box2) und ein listViewEx haben Formular nächste Bindungen:
box1.SelectedItem -> SelectedPerson, box1.ItemsSource -> Personen
box2.SelectedItem -> SelectedPerson, box2.ItemsSource -> Personen
listViewEx.SelectedItem -> SelectedPerson, listViewEx.ItemsSource -> Personen
Form hat einen gültigen DataContext-Wert mit entsprechenden Eigenschaften und Werten für SelectedPerson und Personen.
Nun ist die Frage: warum alle Bindungen außer gut funktioniert diese:
listViewEx.SelectedItem -> SelectedPerson
Wenn i Werte in Comboboxen ändern - als Auswahl in listViewEx bis i chaged Ändern Sie die Auswahl in ListViewEx, nach diesen Kombinationsfeldern funktioniert gut, aber listViewEx verliert alle Bindungen. Was mache ich falsch?
Problem reproduction video (gif)
listViewEx Code:
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;
}
}
Formularcode:
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 Form:
<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>
Vielen Dank! Deine Lösung funktioniert! Es ist so ein dummer Fehler, der mir viel Zeit gekostet hat. –