简体   繁体   中英

WPF control binding to previous binding before DataTrigger overrides it

I have the following question : what do I need to add in the code below to tell my RadioButton to bind on "IsFacturation" boolean that is attached to datagrid item ? I use a DataTrigger which defines its own binding onto datagrid readonly state, so I need to "get back" in binding definition, probably by looking at appropriate parent. I think I have to play with RelativeSource...

I observe that when a datagrid item has IsFacturation boolean set to true, the radio button isn't checked as it should be.

DataGrid items are an observable collection of "Adresse" objects, which define an "IsFacturation" property.

    <DataGrid x:Name="AddressGrid" SelectionUnit="Cell" ItemsSource="{Binding Path=Adresses}" SelectionMode="Single">
        <DataGrid.Columns>
                <!-- Region Facturation -->
            <DataGridTemplateColumn Header="Facturation" SortMemberPath="IsFacturation" HeaderStyle="{StaticResource CenterAlignmentColumnHeaderStyle}" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <ContentControl>
                            <ContentControl.Style>
                                <Style TargetType="{x:Type ContentControl}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ElementName=AddressGrid,Path=IsReadOnly}" Value="True">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <!-- Possibly create another contentcontrol which differentiates between errors -->
                                                    <DataTemplate>
                                                        <Image Source="Resources/Images/Check-icon.png" Visibility="Visible"/>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding ElementName=AddressGrid,Path=IsReadOnly}" Value="False">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <RadioButton GroupName="grpRadioButtonFacturationAddresses" 
                                                            IsChecked="{Binding Path=IsFacturation, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Visible"/>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

The goal of such code is to display an image when datagrid is readonly, and a radio button when it's not. I still have to work on image visibility (easy), but radio button state is directly linked to datagrid item property of my choice.

Thanks a lot

This is just answering your comment, not your question. One way that you could use DataTrigger s without a ContentControl is to move them to the actual controls:

<DataTemplate>
    <Grid>
        <Image Source="Resources/Images/Check-icon.png" Visibility="Visible">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Visibility="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=AddressGrid,Path=IsReadOnly}" Value="True">
                            <Setter Property="Visibility" Value="Visible">
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <RadioButton GroupName="grpRadioButtonFacturationAddresses" IsChecked="{Binding Path=IsFacturation, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center">
            <RadioButton.Style>
                <Style TargetType="{x:Type RadioButton}">
                    <Setter Visibility="Visible" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=AddressGrid,Path=IsReadOnly}" Value="True">
                            <Setter Property="Visibility" Value="Collapsed">
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RadioButton.Style>
        </RadioButton>
    </Grid>
</DataTemplate>

I think I found the cause of our problem.

I suppress the ContentControl and the binding of my radiobutton working now.

Edit: Oh, I had not seen your response Sheridan :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM