无法解析CellTemplate绑定中的符号

时间:2018-07-27 07:59:13

标签: c# wpf data-binding

我有一个绑定到Producers属性的ListView控件ItemsSource,它运行良好。但是,当我尝试添加一些DataTriggers时,却收到Cannot resolve symbol 'Up' VS警告。
代码有效,但是我想通过明确指定绑定源来摆脱警告。

我尝试使用RelativeSource,但发现CellTemplate无法使用相对源,因为它不是VisualTree的一部分。

<ListView ItemsSource="{Binding Producers, ElementName=This}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"></GridViewColumn>
            <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}"></GridViewColumn>
            <GridViewColumn Header="Up">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Up}">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Up}" Value="True">
                                            <Setter Property="Foreground" Value="Green"></Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Up}" Value="False">
                                            <Setter Property="Foreground" Value="Red"></Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>  

This元素定义为:

<Window x:Class="Helper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Helper"
        mc:Ignorable="d"
        Title="MainWindow" Height="900.731" Width="889.344"
        x:Name="This">
    <Grid>
        ...
    </Grid>
</Window>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

尝试在设计模式下设置DataContext。像d:DataContext="{d:DesignInstance vm:ViewModel}"

对于您而言,我认为应该是d:DataContext="{d:DesignInstance model:Producer}"

简单将其设置为您的TextBlock

<TextBlock Text="{Binding Up}" d:DataContext="{d:DesignInstance model:Producer}">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Up}" Value="True">
                    <Setter Property="Foreground" Value="Green"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Up}" Value="False">
                    <Setter Property="Foreground" Value="Red"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>