J'ai un ParentViewModel qui a ObservableCollection nommé Notices. J'ai des contrôles statiques au contrôle de l'utilisateur où je veux lier cette observablecollection à ces contrôles statiques. Et je ne sais pas comment lier ces avis en fonction de ses types. Je veux lier un avis avec Type FirstType à "première ligne" et notez avec Type SecondType à "deuxième ligne" Aussi si la case à cocher de l'utilisateur checkbox Avis doit être retiré de la collection.Liaison en fonction du type dans WPF
Il y a mes codes
<UserControl x:Class="Repo.UserControlNotices"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="Auto">
<Grid DataContext="{Binding}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Text="FirtNotice" >
</TextBox>
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBox Text="{Binding Path =Content}" >
</TextBox>
<CheckBox >
</CheckBox>
</StackPanel>
<TextBox Grid.Row="1" Text="SecondNotice">
</TextBox>
<StackPanel Grid.Row="1" Grid.Column="1">
<TextBox Text="{Binding Path =Content}" >
</TextBox>
<CheckBox >
</CheckBox>
</StackPanel>
</Grid>
</UserControl>
class ParentViewModel
{
public ObservableCollection<Notice> Notices { get; set; }
public ParentViewModel()
{
ObservableCollection<Notice> loadedNotices = new ObservableCollection<Notice>();
loadedNotices.Add(new Notice() { Content = "Something", Type = NoticeType.FirstType });
loadedNotices.Add(new Notice() { Content = "Something", Type = NoticeType.SecondType });
Notices = loadedNotices;
}
}
<Window x:Class="Repo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Repo="clr-namespace:Repo" Title="Window1" Height="300" Width="300">
<Window.Resources>
<Repo:ParentViewModel x:Key="parentVM"/>
</Window.Resources>
<Window.DataContext>
<StaticResourceExtension ResourceKey="parentVM"/>
</Window.DataContext>
<Grid>
<Repo:UserControlNotices DataContext="{Binding Path=Notices}">
</Repo:UserControlNotices>
</Grid>
</Window>
Ceci est par exemple dans WinForms: alt text http://img441.imageshack.us/img441/6487/examplese.png
Si vous faites cela, je suppose que vous devriez aussi mettre un .ToList() à la fin de votre Where. Sinon, la liaison peut ignorer la propriété PropertyChanged pour des raisons que l'expression * que vous renvoyez n'a pas changé. Au moins, j'ai eu beaucoup de tels problèmes contraignants qui ont abouti à cela dans le passé. –