数据触发器不工作

DataTrigger is not working

我正在学习如何在 WPF 中创建自定义控件,但是我遇到了有关如何使用 DataTrigger 的问题。

我的组件基本上是一个组合框,我在它的顶部添加了 3 个单选按钮。

这是我正在尝试做的事情: 我创建了一个枚举:

public enum TipoTree
    {
        Materiais,
        Produtos,
        Servicos
    }

此外,我有一个名为 GIComboTree 的 class,它有一个 属性,我将其命名为 "TipoTreeProperty":

PS:_vm 是我从中获取 ItemSource 的对象。我通过这个 _vm.

将项目添加到我的 ComboBox
public static DependencyProperty TipoTreeProperty =
            DependencyProperty.Register(
                "TipoTreeP",
                typeof(TipoTree),
                typeof(GIComboTree),
                new PropertyMetadata(TipoTree.Materiais));

public TipoTree TipoTreeP 
        {
            get { return (TipoTree)GetValue(TipoTreeProperty); }
            set 
            { 
                SetValue(TipoTreeProperty, value);
                _vm = null;
                _vm = new GIComboTreeVM(this.TipoTreeP);
                this.UpdateLayout();
            }
        }

我想要的是:选中其中一个单选按钮时,从组合框中更改上下文。

例如:如果选中radMateriais,则ComboBox 项目更改为"AAAA"。如果选中 radProdutos,则 ComboBox 项更改为 "BBBB"。如果选中 radServicos,组合框项目将更改为 "CCCC"。但 DataTrigger 不是 "firing",因此 ComboBox 的项目不会改变。 这是我的样式代码的一部分(只是 GIComboTree 样式):

<Style x:Key="GIComboTreeStyle" TargetType="{x:Type gi:GIComboTree}" >
        <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
        <Setter Property="Width" Value="220" />
        <Setter Property="MinWidth" Value="220" />
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="Padding" Value="4,3"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type gi:GIComboTree}">
                    <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="25" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <StackPanel Margin="0 5 0 5" Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal" >
                            <gi:GIRadioButton Margin="1 0 0 0" IsChecked="True" Content="Materiais" Tag="10" GroupName="Descr" x:Name="radMateriais" />
                            <gi:GIRadioButton Margin="10 0 0 0" Content="Produtos" Tag="13" GroupName="Descr" x:Name="radProdutos" />
                            <gi:GIRadioButton Margin="10 0 0 0" Content="Servi&#231;os" Tag="15" GroupName="Descr" x:Name="radServicos" />
                            <!-- &#231; = ce-cedilha -->
                        </StackPanel>
                        <Popup Grid.Row="1" x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                            <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
                                <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                                    <ScrollViewer x:Name="DropDownScrollViewer">
                                        <Grid RenderOptions.ClearTypeHint="Enabled">
                                            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                                <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                                            </Canvas>
                                            <gi:ExtendedTreeView x:Name="treeView" BorderThickness="0" ItemContainerStyle="{StaticResource MyTreeViewItemStyle}" 
                                                                    ItemsSource="{TemplateBinding ItemsSource}" ItemTemplate="{TemplateBinding ItemTemplate}" />
                                        </Grid>
                                    </ScrollViewer>
                                </Border>
                            </Themes:SystemDropShadowChrome>
                        </Popup>
                        <ToggleButton Grid.Row="1" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
                        <ContentPresenter Grid.Row="1" x:Name="ContentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>


                        <DataTrigger Binding="{Binding ElementName=radMateriais, Path=IsChecked, Mode=TwoWay}" Value="True">
                            <Setter Property="TipoTreeP" Value="Materiais"/>
                        </DataTrigger>

                        <DataTrigger Binding="{Binding ElementName=radProdutos, Path=IsChecked, Mode=TwoWay}" Value="True">
                            <Setter Property="TipoTreeP" Value="Produtos"/>
                        </DataTrigger>

                        <DataTrigger Binding="{Binding ElementName=radServicos, Path=IsChecked, Mode=TwoWay}" Value="True">
                            <Setter Property="TipoTreeP" Value="Servicos"/>
                        </DataTrigger>



                        <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                            <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                            <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
                        </Trigger>
                        <Trigger Property="HasItems" Value="false">
                            <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            <Setter Property="Background" Value="#FFF4F4F4"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsGrouping" Value="true"/>
                                <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </MultiTrigger>
                        <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
                            <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                            <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate" Value="{StaticResource treeViewDataTemplate}" />
        <Style.Triggers>
            <Trigger Property="IsEditable" Value="true">
                <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
                <Setter Property="IsTabStop" Value="false"/>
                <Setter Property="Padding" Value="3"/>
                <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

在 Datatrigger 中,您将枚举值绑定为普通字符串。它应该遵循语法 as

{x:Static namespace:ClassName+EnumName.EnumValue}

将您的数据触发器更改为

<DataTrigger Binding="{Binding ElementName=radMateriais, Path=IsChecked, Mode=TwoWay}" Value="True">
                            <Setter Property="TipoTreeP" Value="{x:Static my:TipoTree.Materials}"/>
                        </DataTrigger>