4

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

Cyril
  • 43
  • 1
  • 1
  • 4
  • What happens? What is P_DAY_PRICE? Is P_DAY_PRICE a member of the DG's data context? You don't give us enough information to help you. – CodingGorilla Mar 29 '11 at 15:00
  • P_DAY_PRICE is a field from my datatable. i filled dtGrdData.ItemsSource with the result of storedProcedure – Cyril Mar 29 '11 at 15:04
  • 2
    The data trigger will use the current `DataContext` not the data in the `ItemsSource`. You probably need to properly configure the DataContext of the grid. – CodingGorilla Mar 29 '11 at 15:07

2 Answers2

13

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>
Snowbear
  • 16,924
  • 3
  • 43
  • 67
3

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

Riana
  • 689
  • 6
  • 22