【问题标题】:How to use trigger in datatemplate in WPF如何在 WPF 的数据模板中使用触发器
【发布时间】:2017-03-16 17:32:18
【问题描述】:

如何根据 IsChecked 属性更改复选框中当前选定元素图标的属性填充?

我的资源字典:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- (...) --> 
        </ResourceDictionary.MergedDictionaries>
        <DataTemplate x:Key="FooTemplate">
            <Icons:ExIcon Fill="Red"
                          Width="12"
                          Height="Auto">
                <!--
                <Icons:ExIcon.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={???}}, Path=???}" Maybe here?
                       Value="???"/>
                </Icons:ExIcon.Triggers>
                -->
            </Icons:ExIcon>
        </DataTemplate> 
        <!-- (...) -->
        <styles:IconSelector x:Key="IconSelector"
                             FooTemplate="{StaticResource FooTemplate}"
                             FooTemplateSSecond="{StaticResource FooTemaplteSecond}"/>

还有列表框:

<ListBox ItemsSource="{Binding DataSources}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <!-- (...) --> 
                <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}">
                    <CheckBox.Template>
                        <!-- (...) --> 
                        <ContentControl Name="Icon"
                                        Content="{Binding}"
                                        ContentTemplateSelector="{StaticResource IconSelector}"
                                        HorizontalAlignment="Right"
                                        Grid.Column="1"/>
                        <!-- (...) --> 
                    </CheckBox.Template>
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

有可能吗?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我并不完全清楚您的CheckBox.Template 是什么样子。 Icons:ExIcon Control 对我来说也是个谜。无论如何,这是一个小的工作示例。它使用矩形而不是您的 ExIcon。但您可以轻松替换它;-) 我通过 DataTrigger 直接绑定到 IsSelected,而不是通过 ElementNameRelativeSource 搜索。

    XAML:

           <ListBox ItemsSource="{Binding}">
            <ListBox.Resources>
                <DataTemplate x:Key="template1">
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding Path=IsSelected}" />
                        <Rectangle Height="20"
                                   Width="20">
                            <Rectangle.Style>
                                <Style TargetType="Rectangle">
                                    <Setter Property="Fill"
                                            Value="Blue" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsSelected}"
                                                     Value="True">
                                            <Setter Property="Fill"
                                                    Value="Red" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Rectangle.Style>
                        </Rectangle>
                    </StackPanel>
                </DataTemplate>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ContentControl ContentTemplate="{StaticResource template1}" Content="{Binding}">
    
                    </ContentControl>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    注意 Content="{Binding}"。这会将ContentPresenterDataContext 设置为ListBoxItem 的“实际”DataContext。如果您使用 VS2015,您可以使用 VisualTreeViewer 进行调查,否则您可以使用 https://snoopwpf.codeplex.com/

    您还应该考虑将IsSelected重命名为IsChecked。通常,您使用IsSelected 属性来指示该行是否被选中。

    【讨论】:

    • 不幸的是,这并不能解决问题。根据列表中的一种元素,应用不同的模板,模板必须在 ResourceDictionary 中定义。例如:列表显示员工。如果员工是经理,则显示“A”图标。如果员工是董事,则显示“B”图标……以此类推。如果选中该项,我想更改 ContentControl 子项的颜色(有时是“A”,有时是“B”)。
    • 重要的是图标是ContentControl的内容。
    • 您可以为不同的 ViewModel 使用不同的 DataTemplates &lt;DataTemplate DataType="{mynamespace:directorViewModel}"&gt;。那么您甚至不需要 ContentTemplateSelector。数据模板自动作为 ItemTemplate 应用。另请参阅stackoverflow.com/questions/19864891/…。无论如何,我将示例更新为 ContentControl
    • 这提示有帮助。谢谢!
    猜你喜欢
    • 2011-09-09
    • 2011-08-29
    • 2011-08-11
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2016-03-12
    相关资源
    最近更新 更多