WPF ContentControl using DataTriggers - UserControl content displays in one row, but removes content from another row RRS feed

  • Question

  • I have multiple combo boxes in a view, each on its own row and bound to the same ItemsSource. They have an element name to distinguish them from one another. In my view, under window.resources, I have a list of user controls defined as data templates. What I would like to do is when a user selects an item from a combo box, it displays the contents of one of the seven user controls in the column adjacent to the corresponding combo box based on the combo box selection. I have done this using data triggers defined within a tag. The issue is when I make a selection in one combo box that happens to be the same selection as that of another combo box, the content appears adjacent to the most recent selected combo box, but disappears from the other one.

    I have defined a content control with a style and data triggers and I can see that the content is updating correctly as long as I have only one combo box. However, it does not work with more than one combo box. I don't understand how even though I have the DataTrigger Binding elementname set to the name of a specific combo box, making a selection on that combo box affects the content of another row which is tied to a different element name and combo box.

     <local:OneLayout x:Key="OneLayout" />
     <DataTemplate DataType="{x:Type local:OneLayout}"  >
     <local:TwoLayout x:Key="TwoLayout" />
     <DataTemplate DataType="{x:Type local:TwoLayout}"  >
    <ComboBox x:Name="Layout1" Margin="5" Grid.Row="0" Grid.Column="1" 
        ItemsSource="{Binding LayoutTypes }" DisplayMemberPath="Name"/>
    <ContentControl Grid.Row="0" Grid.RowSpan="3" Grid.Column="2">
           <Style TargetType="{ x:Type ContentControl }" >
            <Setter Property="Content" Value=" 
                  {StaticResource ResourceKey=OneLayout}" />
                   <DataTrigger Binding="{Binding 
                       SelectedItem.Layout, ElementName=Layout1}" 
                    <Setter Property="Content" 
                      Value="{StaticResource ResourceKey=OneLayout}" />
                   <DataTrigger Binding="{Binding 
                       SelectedItem.LayoutType, ElementName=Layout1}" 
                    <Setter Property="Content" 
                      Value="{StaticResource ResourceKey=TwoLayout}" />
    <ComboBox x:Name="Layout2" Margin="5" Grid.Row="3" Grid.Column="1" 
             ItemsSource="{Binding LayoutTypes }" DisplayMemberPath="Name"/>
    <ContentControl Grid.Row="3" Grid.RowSpan="3" Grid.Column="2" >
        ... //same as content control above except for    

    I expect to have the user control associated with the combo box selection appear adjacent to the combo box that was selected and not to affect the content in other rows.

    Any suggestions would be greatly appreciated.  Thanks!

    • Moved by Dave PatrickMVP Tuesday, September 17, 2019 6:05 PM not reporting forums application issues
    • Moved by Dave PatrickMVP Tuesday, September 17, 2019 8:59 PM account verification request
    Tuesday, September 17, 2019 5:47 PM


All replies