C# 绑定取决于WPF中的类型

C# 绑定取决于WPF中的类型,c#,wpf,xaml,mvvm,binding,C#,Wpf,Xaml,Mvvm,Binding,我有一个ParentViewModel,它具有名为Notices的ObservableCollection public class Notice { public string Content { get; set; } public NoticeType Type { get; set; } } 我在用户控件中有静态控件,我想将这个observablecollection绑定到这个静态控件。 我不知道如何根据它的类型绑定它。 我想将类型

我有一个ParentViewModel,它具有名为Notices的ObservableCollection

public class Notice
    {

        public string Content { get; set; }

        public NoticeType Type { get; set; }

    }
我在用户控件中有静态控件,我想将这个observablecollection绑定到这个静态控件。 我不知道如何根据它的类型绑定它。 我想将类型为FirstType的通知绑定到“第一行”,将类型为SecondType的通知绑定到“第二行” 此外,如果用户选中复选框,则应从集合中删除通知

这是我的密码

<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>

类ParentViewModel
{
公共可观察收集通知{get;set;}
公共ParentViewModel()
{
ObservableCollection loadedNotices=新的ObservableCollection();
loadedNotices.Add(newnotice(){Content=“Something”,Type=NoticeType.FirstType});
loadedNotices.Add(newnotice(){Content=“Something”,Type=NoticeType.SecondType});
通知=加载的通知;
}
}
这是winforms中的示例:
我不确定您的要求,但我认为您最有可能需要使用IValueConverter接口。您可以声明一个实现IValueConverter接口的新类,并实现如下所示的Convert和ConvertBack方法

public Converter implements IValueConverter{
      Object Convert(value, targetType, parameter, culture){
            // 'value' has the bound value from the xaml
            // do some converting and return the result
      }
      Object Convert(value, targetType, parameter, culture){
            // you can figure this one out, usually used for a two-way relationship
      }

}
在xaml中,在xaml中需要一个ResourceDictionary,其中包括

xmlns:y="clr-namespace:NamespaceWithClass">
<y:Converter x:Key="someName" />
xmlns:y=“clr命名空间:NamespaceWithClass”>
并在控件中引用此转换器

<TextBox Text="{Binding Content, Converter={StaticResource someName}}" >           
            </TextBox>  


不确定这是否正是您想要的,但我希望它能有所帮助。

在ParentViewModel上只拥有两个属性,例如

public static IEnumerable<Notice> FirstTypeNotices
{
    get
    {
        return Notices.Where(n => n.Type == NoticeType.FirstType);
    }
}
公共静态IEnumerable FirstTypeNotices
{
得到
{
返回通知。其中(n=>n.Type==NoticeType.FirstType);
}
}
…并与之绑定


您可能必须连接到Notices.CollectionChanged事件,并为这两个属性中的每一个引发PropertyChanged事件。

如果这样做,我想您还需要在Where的末尾添加一个.ToList()。否则,绑定可能会忽略PropertyChanged,因为返回的表达式没有更改。至少,我在过去遇到过很多这样的约束问题。