【问题标题】:DataTrigger is not working数据触发器不工作
【发布时间】:2015-06-10 01:54:15
【问题描述】:

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

我的组件基本上是一个 ComboBox,我在它的顶部添加了 3 个 RadioButtons。

这就是我想要做的事情: 我创建了一个枚举:

public enum TipoTree
    {
        Materiais,
        Produtos,
        Servicos
    }

另外,我有一个名为 GIComboTree 的类,它有一个我命名为“TipoTreeProperty”的属性:

PS:_vm 是我从中获取 ItemSource 的对象。我通过这个 _vm 将 Items 添加到我的 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,则 ComboBox 项目将更改为“CCCC”。但是 DataTrigger 没有“触发”,所以 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>

【问题讨论】:

    标签: wpf combobox treeview radio-button custom-controls


    【解决方案1】:

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

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

    将您的 Datatrigger 更改为

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

    【讨论】:

    • 我更改了我的 DataTrigger 但它仍然无法正常工作。当我检查单选按钮时,DataTrigger 没有触发。我在我的属性 TipoTreeP 上放了一个断点,但程序并没有停在那里。
    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2013-12-04
    • 2022-10-14
    • 2010-11-14
    • 2013-03-27
    相关资源
    最近更新 更多