根据listboxitem值

时间:2017-01-05 12:20:30

标签: wpf binding listbox

我想根据布尔属性ListBoxItem的值设置DummyQuestion的颜色,该颜色来自Answered的类型 这就是我所做的,但它没有工作,我认为我的绑定路径有问题

<ListView>
<ListView.ItemTemplate>
    <DataTemplate DataType="{x:Type local:DummyQuestion}">
        <Grid>
            <Ellipse Width="45" Height="45">
                <Ellipse.Style>
                    <Style TargetType="Ellipse">
                        <Style.Setters>
                            <Setter Property="Fill" Value="{StaticResource BaseGrey}" />
                        </Style.Setters>
                        <Style.Triggers>
                            <DataTrigger 
                                Binding="{Binding 
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, 
                                Path=Answered}" 
                                Value="True">
                                <Setter Property="Fill" Value="{StaticResource BaseGreen}" />
                            </DataTrigger>
                            <DataTrigger 
                                Binding="{Binding 
                                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, 
                                Path=Marked}" 
                                Value="True">
                                <Setter Property="Fill" Value="{StaticResource Accent}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Ellipse.Style>
            </Ellipse>
            <TextBlock Text="{Binding SerialNumber}" />
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>
<local:DummyQuestion
    Answered="False"
    ArText="xxxx"
    Marked="False"
    SerialNumber="1"
    Text="xxxx?" />
<local:DummyQuestion
    Answered="True"
    ArText="yyyy"
    Marked="False"
    SerialNumber="2"
    Text="yyyy" />
<local:DummyQuestion
    Answered="False"
    ArText="zzzz"
    Marked="False"
    SerialNumber="3"
    Text="zzzz" />

这是DummyQuestion类:

public class DummyQuestion
{
    public string SerialNumber { get; set; }
    public string Text { get; set; }
    public string ArText { get; set; }
    public bool Answered { get; set; }
    public bool Marked { get; set; }
}

1 个答案:

答案 0 :(得分:0)

不需要添加relativesource,从绑定表达式中删除relativesource。