简体   繁体   中英

Style DataGridCell with ComboBox based on Header value

We have a user control that displays a combo box of available SQL tables the user can view. Each table contains different data, number of columns, etc. so the grid is auto generated. The DataView is bound to the grid is repopulated via a SQL call when the user selects a different table from the combobox.

I now have a request that for just one of the tables, the column called SourceCodeType be shown as a combobox of just a few values (L1, L2, L3).

I have the following style (and VM property TransposeCodeTypeComboBoxValues) which displays the ConboBox with the list of values:

<Window.Resources>

    <ResourceDictionary>

            <CollectionViewSource x:Key="TransposeCodeTypeCollection"
                                  Source="{Binding RelativeSource={RelativeSource FindAncestor, 
                                            AncestorType={x:Type Window}}, Path=DataContext.TransposeCodeTypeComboBoxValues}" />

            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.Header, Mode=OneWay,

                                            Converter={StaticResource StringComparisonToBooleanConverter}, ConverterParameter='SourceCodeType'}" Value="True">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ComboBox Name="TransposeCodeComboBox"
                                              IsReadOnly="True"
                                              ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
                                              Width="150" />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

    </ResourceDictionary>
</Window.Resources>

...

<StackPanel>

                <DataGrid Name="AdjustmentTablesDataGrid" 
                          ItemsSource="{Binding AdjustmentTableDataView, UpdateSourceTrigger=PropertyChanged}"
                          SelectedItem="{Binding SelectedProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          Margin="0,20,0,10"
                          AutoGenerateColumns="True"
                          ColumnWidth="*"
                          CanUserAddRows="False"
                          MaxHeight="500"
                          EnableRowVirtualization="True"
                          IsEnabled="True">

I would like to know how I can bind SourceCodeType in the table to the correct combobox value when the table is loaded/displayed.

EDIT

I found that I can bind to the SourceCodeType using this ComboBox in the style:

<ComboBox Name="TransposeCodeComboBox"
          IsReadOnly="True" 
          ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
          SelectedItem="{Binding SourceCodeType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          Width="150" />

The problem I still have is that this appears to be binding to the last row's SourceCodeType and if I change one combobox, they all change to that newly selected value.

How do I get the style to bind correctly to each individual row of data?

Through another post on a different topic I found the answer to this:

        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.Header, Mode=OneWay, 
                                        Converter={StaticResource StringComparisonToBooleanConverter}, ConverterParameter=DestinationCodeType}" Value="True">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                               <ComboBox Name="TransposeCodeComboBox"
                                         IsReadOnly="True" 
                                         ItemsSource="{Binding Source={StaticResource TransposeCodeTypeCollection}}"
                                         SelectedValue="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=DataContext.DestinationCodeType, Mode=TwoWay}"
                                         IsSynchronizedWithCurrentItem="False"
                                         Margin="0"
                                         Width="Auto"
                                         Height="Auto" />

                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

The big thing I was missing was the IsSynchronizedWithCurrentItem="False" .

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