How to set cell template element's dependency property upon row selection

291 Views Asked by At

I have a custom UI element inside a CellTempate (VsInputControlNumeric). I want to set this element's IcFontColor property using a trigger when it's row is selected.

<DataGridTemplateColumn Header="{x:Static lp:LanguagePack._Threshold}" IsReadOnly="False" Width="Auto" MinWidth="100" SortMemberPath="Threshold.NumericValue">
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCellLeftText}">
            <Setter Property="Focusable" Value="True"/>
            <Setter Property="Background" Value="{Binding Path=BackColor}"/>
        </Style>
    </DataGridTemplateColumn.CellStyle>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <view:VsInputControlNumeric
                    IcControlType="{x:Static configurator:VisualElementType.Numeric}" 
                    IsEnabled="False"
                    IcFontColor="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.FontColor, UpdateSourceTrigger=PropertyChanged, FallbackValue=Black}"
                    IcBindingValue="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.Threshold.RawValue, UpdateSourceTrigger=PropertyChanged, FallbackValue={x:Null}}"
                    IcTextValue="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.Threshold.StringValue, UpdateSourceTrigger=PropertyChanged, FallbackValue={x:Null}}"
                    HorizontalContentAlignment="Right" ControlHeight="25" BorderThickness="1" Background="{Binding Path=BackColor}"
                >
            </view:VsInputControlNumeric>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

How could I accomplish this? (Currently I set it from the ViewModel)

Much thanks in advance...

1

There are 1 best solutions below

0
On

Confusingly enough you can bind to a DataGridRow property using a DataTrigger and not a (property)Trigger. The solution that worked ended up being the following:

<DataGridTemplateColumn Header="{x:Static lp:LanguagePack._Threshold}" IsReadOnly="False" Width="Auto" MinWidth="100" SortMemberPath="Threshold.NumericValue">
<DataGridTemplateColumn.CellStyle>
    <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCellLeftText}">
        <Setter Property="Focusable" Value="True"/>
        <Setter Property="Background" Value="{Binding Path=BackColor}"/>
    </Style>
</DataGridTemplateColumn.CellStyle>
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <view:VsInputControlNumeric
                IcControlType="{x:Static configurator:VisualElementType.Numeric}" 
                IsEnabled="False"
                IcBindingValue="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.Threshold.RawValue, UpdateSourceTrigger=PropertyChanged, FallbackValue={x:Null}}"
                IcTextValue="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext.Threshold.StringValue, UpdateSourceTrigger=PropertyChanged, FallbackValue={x:Null}}"
               HorizontalContentAlignment="Right" 
                ControlHeight="25" 
                BorderThickness="1">
            <view:VsInputControlNumeric.Style>
                <Style TargetType="{x:Type view:VsInputControlNumeric}">
                    <Setter Property="IcFontColor" Value="{StaticResource MyBlack}"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}"
                                     Value="True">
                            <Setter Property="IcFontColor" Value="{StaticResource BorderLightBrush}"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}"
                                     Value="False">
                            <Setter Property="IcFontColor" Value="{StaticResource MyBlack}"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </view:VsInputControlNumeric.Style>
        </view:VsInputControlNumeric>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>