Search code examples
c#wpfxamldatagrid

ContentControl in datagrid with Style.Trigger disappearing


I have a datagrid with two columns. First column contains cells with a combobox with two possible values (KoSt, WBKZ). Depending on that value column2 should show a different contentcontrol (textbox, autocompletebox):

values

If i choose more than one time the same value in column one than all other controls in column2 which will match to that value will disappear (except the current row). missing content Why does all of the other content dissappear? :(

                <DataGrid x:Name="dataGridAccounting"
    ItemsSource="{Binding CurrentAction.ACCOUNTING_COLLECTION, NotifyOnValidationError=True}"
    AutoGenerateColumns="False">
    <DataGrid.Columns>
        <!-- first column binded to a static list with two values (KoSt, WBKZ)-->
        <DataGridTemplateColumn Header="Konten-Art">
            <!-- shows the value of column1-->
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding KONTEN_ART, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>

            <DataGridTemplateColumn.CellEditingTemplate>
                <!-- lets the user click an item in combobox -->
                <DataTemplate>
                    <ComboBox ItemsSource="{StaticResource AccountingTypeList}"
                              SelectedItem="{Binding KONTEN_ART, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

        <!-- second column. Content disappear when selecting more than one time same value in column 1-->
        <DataGridTemplateColumn Header="content">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ContentControl>
                        <ContentControl.Style>
                            <Style TargetType="ContentControl">
                                <Style.Triggers>

                                    <DataTrigger Binding="{Binding KONTEN_ART}" Value="KoSt">
                                        <Setter Property="Content">
                                            <Setter.Value>
                                                <toolkit:AutoCompleteBox Text="Value1 was chosen"></toolkit:AutoCompleteBox>
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding KONTEN_ART}" Value="WBKZ">
                                        <Setter Property="Content">
                                            <Setter.Value>
                                                <TextBox Text="Value2 was chosen"></TextBox>
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContentControl.Style>
                    </ContentControl>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Solution

  • Instead of setting Content of ContentControl in the DataTrigger, set the ContentTemplate like below

       <DataTrigger Binding="{Binding KONTEN_ART}" Value="KoSt">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <toolkit:AutoCompleteBox Text="Value1 was chosen"></toolkit:AutoCompleteBox>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding KONTEN_ART}" Value="WBKZ">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox Text="Value2 was chosen"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>