首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在EventTrigger命令完成之前,XAML样式不适用

在EventTrigger命令完成之前,XAML样式不适用
EN

Stack Overflow用户
提问于 2015-04-29 15:19:43
回答 1查看 82关注 0票数 0

我有一个ListBox,它定义了一个自定义ControlTemplate。所选项目具有更改背景和前景的样式,并且样式基本工作。但是,我想介绍一种行为,在选择更改时显示一个模态消息框,询问用户是否真的想选择不同的项目。我实现了一个ICommand来完成这个任务,下面的代码显示为AreYouSureCommand。

问题是,在显示模态消息框的同时,所选项目的背景样式会被更改,但前景不会更改。一旦我取消了模态信息框,前景颜色就会发生变化。我还没有包含ICommand的代码,因为它有点复杂,但希望在执行ShowDialog时使用ShowDialog打开窗口就足够了。

有谁能解释一下为什么我的背景颜色会改变,而不是前景颜色呢?

代码语言:javascript
运行
复制
<ListBox x:Name="SubMenu" ItemsSource="{Binding MyItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DisplayName}"
                        Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ContentControl}}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="MainBorder">
                            <ContentControl x:Name="Presenter">
                                <ContentPresenter />
                            </ContentControl>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <!-- Setter on MainBorder applies before AreYouSureCommand completes -->
                                <Setter TargetName="MainBorder" Property="Background" Value="Red" />
                                <!-- Setter on Presenter applies after AreYouSureCommand completes -->
                                <Setter TargetName="Presenter" Property="Foreground" Value="Green" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding AreYouSureCommand}"
                                    CommandParameter="{Binding SelectedItem, ElementName=SubMenu}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-20 08:08:02

我最终找到了解决这个问题的办法。基本上,我为IsSelected属性使用另一个setter将ListViewItemIsSelected值推送到视图模型上。然后,我使用DataTrigger而不是常规触发器来设置选定的样式,并将触发器绑定到视图模型上的IsSelected属性,而不是ListViewItem本身的属性。我真的不知道这是为什么-它不应该有任何不同的真正,但它确实工作。

谢谢胡安和本的评论。

代码语言:javascript
运行
复制
<ListBox x:Name="SubMenu" ItemsSource="{Binding MyItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DisplayName}"
                        Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ContentControl}}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="MainBorder">
                            <ContentControl x:Name="Presenter">
                                <ContentPresenter />
                            </ContentControl>
                        </Border>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                <Setter TargetName="MainBorder" Property="Background" Value="Red" />
                                <Setter TargetName="Presenter" Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding AreYouSureCommand}"
                                    CommandParameter="{Binding SelectedItem, ElementName=SubMenu}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29947999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档