WPF ItemContainerStyle不绑定到集合项

时间:2014-04-22 17:31:56

标签: c# wpf xaml binding

我有一个名为ProjectViewModel的视图模型对象,它被指定为我的Window的数据上下文。在视图模型中,我有一个ObservableCollection<DiaryViewModel>,我绑定到ListView的ItemsSource。在DiaryViewModel内,我有一些属性。 ListViewItem项需要根据这些属性值进行样式化。当我尝试绑定到与DiaryViewModel关联的ListViewItem时,visual studio告诉我该属性不存在。 Resharper的Intellisense表明,绑定与ProjectViewModel相关联。当我输入Path=时,intellisense会向我显示与ProjectViewModel相关联的所有属性。

有人能告诉我,如果我做错了吗? GridColumns正确绑定到DiaryViewModel属性,而不是ItemContainerStyle

<Window x:Class="Pen.Views.PenMain"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:viewModels="clr-namespace:Pen.ViewModels"
        Title="PEN"
        ResizeMode="CanResizeWithGrip"
        mc:Ignorable="d"
        Height="700"
        Width="1000"
        WindowStartupLocation="CenterScreen"
        d:DataContext="{d:DesignInstance Type=viewModels:ProjectViewModel, IsDesignTimeCreatable=True}">

    <Window.DataContext>
        <viewModels:ProjectViewModel />
    </Window.DataContext>

    <ListView Margin="0"
                Name="lvDiaries"
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                VerticalAlignment="Stretch"
                ItemsSource="{Binding Path=DiariesForSelectedProject}"
                SelectedItem="{Binding Path=SelectedDiaryViewModel}"
                MouseDoubleClick="DiariesDoubleClicked"
                SelectionChanged="lvDiaries_SelectionChanged">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="DiaryId"
                                Width="0"
                                DisplayMemberBinding="{Binding Path=Id}" />
                <GridViewColumn Header="Number"
                                Width="50"
                                DisplayMemberBinding="{Binding Path=Number}" />
                <GridViewColumn Header="DiaryDate"
                                Width="90"
                                DisplayMemberBinding="{Binding Path=Date, StringFormat=d}" />
                <GridViewColumn Header="Inspector"
                                Width="150"
                                DisplayMemberBinding="{Binding Path=Inspector}" />
                <GridViewColumn Header="Status"
                                Width="130"
                                DisplayMemberBinding="{Binding Path=Status}" />
            </GridView>
        </ListView.View>

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Status}"
                                    Value="Supervisor Returned">
                        <Setter Property="Foreground"
                                Value="White" />
                        <Setter Property="Background"
                                Value="Red" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Status}"
                                    Value="Office Returned">
                        <Setter Property="Foreground"
                                Value="White" />
                        <Setter Property="Background"
                                Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

</Window>

1 个答案:

答案 0 :(得分:0)

重新启动visual studio修复了这个问题。现在正在解决运行时未解决的绑定问题。