绑定取决于WPF中的类型

时间:2010-07-01 12:42:28

标签: c# wpf xaml mvvm binding

我有一个名为Notices的ObservableCollection的ParentViewModel。

public class Notice
    {

        public string Content { get; set; }

        public NoticeType Type { get; set; }

    }

我在用户控件上有静态控件,我希望将此observablecollection绑定到此静态控件。 而且我不知道这个Notices如何根据其类型进行约束。 我希望将TypeType类型的绑定通知设置为“第一行”,并将TypeType类型注意到“第二行” 此外,如果用户选中复选框,则应从集合中删除。

有我的代码

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

这是winforms中的示例: alt text http://img441.imageshack.us/img441/6487/examplese.png

2 个答案:

答案 0 :(得分:0)

我不确定你的要求,但我认为你很可能需要使用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" />

并在控件中引用此转换器

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

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

答案 1 :(得分:0)

在ParentViewModel上只有两个属性是不容易的,例如......

public static IEnumerable<Notice> FirstTypeNotices
{
    get
    {
        return Notices.Where(n => n.Type == NoticeType.FirstType);
    }
}

...并绑定到那些?

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

相关问题