Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datatrigger Visibility + Null Value

I'm beginner in WPF. I want to set Visibility to Hidden on a Radiobutton when the databind value is equal to Null. I'm using WPF Toolkit. This is my code but it doesn't work :

    <dg:DataGrid x:Name="dtGrdData" HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserSortColumns="False" AutoGenerateColumns="False"
                 RowHeaderWidth="0" RowHeight="50" >
        <DataTrigger Binding="{Binding P_DAY_PRICE}" Value="{x:Null}">
            <Setter Property="RadioButton.Visibility" Value="Hidden"></Setter>
        </DataTrigger>
        <dg:DataGrid.Columns>
            <dg:DataGridTemplateColumn Header="1 day" Width="1.5*" >
                <dg:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <RadioButton x:Name="rdBtnDayPrice" GroupName="grpNmPrice" Content="{Binding Path=P_DAY_PRICE}" Style="{StaticResource toggleStyle}" Checked="RadioButton_Checked"></RadioButton>
                    </DataTemplate>
                </dg:DataGridTemplateColumn.CellTemplate>
            </dg:DataGridTemplateColumn>
        </dg:DataGrid.Columns>
    </dg:DataGrid>

Can you help me ? Thanks

like image 858
Cyril Avatar asked Dec 04 '22 22:12

Cyril


2 Answers

Move your DataTrigger closer to your RadionButton:

<RadioButton ...>
    <RadioButton.Style>
        <Style TargetType="RadioButton">
            <Style.Triggers>
                <DataTrigger Binding="{Binding P_DAY_PRICE}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </RadioButton.Style>
</RadioButton>
like image 102
Snowbear Avatar answered Dec 18 '22 09:12

Snowbear


I suggest you to set your binding directly on the RadioButton and use 'TargetNullValue' property of the Binding object.

<RadioButton x:Name="rdBtnDayPrice" Visibility={Binding Path=P_DAY_PRICE, TargetNullValue=Hidden, Converter=...} GroupName="grpNmPrice" Content="{Binding Path=P_DAY_PRICE}" Style="{StaticResource toggleStyle}" Checked="RadioButton_Checked" 

You will need a converter to convert 'P_DAY_PRICE' value to Visibility enum value and that should do the job.

Riana

like image 44
Riana Avatar answered Dec 18 '22 07:12

Riana