简体   繁体   English

DataTrigger在ListBox.ItemTemplate中不起作用

[英]DataTrigger not working within a ListBox.ItemTemplate

Let me give a brief overview of my issue. 让我简要概述一下我的问题。

I have a Window that has a ViewModel as a data context. 我有一个具有ViewModel作为数据上下文的窗口。 This window also has 2 User Controls within it. 此窗口中还包含2个用户控件。 These user controls have various xaml objects that bind to properties in the ViewModel, and I am having no problems with any other properties. 这些用户控件具有绑定到ViewModel中属性的各种xaml对象,而我对其他任何属性都没有问题。

The issue I am having is that a data trigger that I have created will not fire. 我遇到的问题是,我创建的数据触发器不会触发。 Below you can see the xaml for the data trigger that I have tried: 在下面,您可以看到我尝试过的数据触发器的xaml:

Note: IsBold is a property within the ViewModel that I am using for the Window. 注意: IsBold是ViewModel中用于窗口的属性。 I am under the impression that user controls within a window will inherit the data context from the parent, so I don't think that is my problem. 我的印象是,窗口内的用户控件将继承父级的数据上下文,因此我认为这不是我的问题。

<ScrollViewer >
        <ListBox
            ItemsSource="{Binding Path=Listings}"
            SelectionMode="Single"
            SelectedValue="{Binding Path=SelectedListingItemID}"
            SelectedValuePath="ItemID"
            Grid.IsSharedSizeScope="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock>
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Text" Value="NotTriggered" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsBold}" Value="True">
                                            <Setter Property="Text" Value="Triggered" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>

When I run this, I will get the output statement: 运行此命令时,将得到输出语句:

BindingExpression path error: 'DataContext' property not found on 'object' ''DataRowView' BindingExpression路径错误:在“对象”“ DataRowView”上找不到“ DataContext”属性

This message leads me to believe that I need to move the data trigger to another part of the xaml so that it realizes that the property is from the ViewModel and not the ListBoxItem, but where do I move it? 该消息使我相信,我需要将数据触发器移动到xaml的另一部分,以便它意识到该属性来自ViewModel而不是ListBoxItem,但是该将其移到哪里? Or is that even the right thing to do? 还是那是正确的事情?

I hope I was clear enough in all the necessary areas, but if I wasn't, I can certainly elaborate where needed. 我希望我在所有必要的地方都足够清楚,但是如果不是这样,我当然可以在需要的地方详细说明。

Thanks for your help! 谢谢你的帮助!

The easiest thing to do is to use relative source property , you can check IsBold from your window's dataContext using FindAncestor mode and AncestorType of Window, something like this: 最简单的方法是使用相对源属性 ,您可以使用FindAncestor模式和Window的AncestorType从窗口的dataContext中检查IsBold,如下所示:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Path=IsBold}" Value="True">
    <Setter Property="Text" Value="Triggered" />
</DataTrigger>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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