DataTrigger is not working

240 views Asked by At

I'm learning how to create custom controls in WPF, however I'm having problems about how to use the DataTrigger.

My component is basically one ComboBox and I added 3 RadioButtons on the the top of it.

Here's what I'm trying to do: I created an enum:

public enum TipoTree
    {
        Materiais,
        Produtos,
        Servicos
    }

Also, I have a class called GIComboTree which has a property that I named "TipoTreeProperty":

PS: _vm is an object from where I get my ItemSource. I add Items to my ComboBox through this _vm.

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();
            }
        }

What I want is: when one of the radiobuttons is checked, change de context from the combo box.

For example: If the radMateriais is checked, the ComboBox items change to "AAAA". If the radProdutos is checked, the ComboBox items change to "BBBB". If radServicos is checked, the ComboBox items change to "CCCC". But the DataTrigger is not "firing", so the ComboBox's items don't change. Here's part of my style code (Just the GIComboTree style):

<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>
1

There are 1 answers

1
ReeganLourduraj On BEST ANSWER

In Datatrigger you are binding enum values as oridinary string. it should follow syntax as

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

Change your Datatrigger as

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