简体   繁体   中英

WPF ItemContainerStyle not binding to collection items

I have a view model object called ProjectViewModel that is assigned as my Window's data context. Within the view model, I have an ObservableCollection<DiaryViewModel> that I bind to the ItemsSource of a ListView . Within the DiaryViewModel I have a few properties. The ListViewItem items need to be stylized based on what those property values are. When I try to bind to the DiaryViewModel associated with the ListViewItem , visual studio tells me that the property does not exist. Resharper's Intellisense show's me that the binding is associated with the ProjectViewModel . When I type Path= the intellisense shows me all of the properties assocaited with the ProjectViewModel .

Can anyone tell me if I am doing something wrong? The GridColumns are bound properly to the DiaryViewModel properties, just not the 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>

Restarting visual studio fixed this problem. The unresolved binding issues at runtime are now properly resolving.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM