I have a ParentViewModel which has ObservableCollection named Notices.
public class Notice
{
public string Content { get; set; }
public NoticeType Type { get; set; }
}
I have static controls at user control where I want to bind this observablecollection to this static controls. And I don't know how binding this Notices depending of its Types. I want bind notice with Type FirstType to "first row" and notice with Type SecondType to "second row" Also If user check checkbox Notice should be removed from collection.
There are my 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>
This is example in winforms: