Devin pam

Devin pam

  • NA
  • 4
  • 679

Why does every combobox has the same checked value?

Jan 4 2023 1:35 PM

I have an an DataGrid in which I load Data from an ObservableCollection and each row has an CheckBox so that I can have a list of checked Rows. Now I wanted to add an new Column to the DataGrid which is an ComboBox and in the ComboBox is a list with Checkboxen. I want for each row that when I check one Row and then when I check other Items from the ComboBox that I can combine the selected row and the selected values from my ComboBox.

The problem is that if I check one Row of the Combobox item it will check for every other ComboBox that appears in the DataGrid.

I want only to check the value of the combobox for one row and combine it with the Value of the checked row of the datagrid.

Here is my Code of my DataGrid:

<DataGrid  Style="{DynamicResource DataGridStyle}" 
   Margin="10"  SelectionUnit="CellOrRowHeader"  AutoGenerateColumns="False" 
   CanUserAddRows="False" CanUserDeleteRows="False" SelectionMode="Single"  VerticalAlignment="Stretch"
   x:Name="SpaltenGrid" ItemsSource="{Binding bischenanders, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
   GridLinesVisibility="None"  ScrollViewer.CanContentScroll="True" 
   ScrollViewer.VerticalScrollBarVisibility="Auto" 
   ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <DataGrid.Columns >
        <DataGridCheckBoxColumn x:Name="selectedField" Binding="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Auswahl" CanUserSort="False">
            <DataGridCheckBoxColumn.ElementStyle>
                <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}" />
            </DataGridCheckBoxColumn.ElementStyle>
        </DataGridCheckBoxColumn>
        
        <DataGridTextColumn  Header="Spaltenname"  Binding="{Binding Spaltenname}" Width="1.5*"/>
        
        <DataGridComboBoxColumn x:Name="cmbAdditionColume"   Header="AdditionsSpalten" DisplayMemberPath="Spaltenname"
                                CanUserSort="False" Width="*">
        
            <DataGridComboBoxColumn.ElementStyle>
                
                <Style TargetType="ComboBox"  BasedOn="{StaticResource ComboBoxStyle1 }"  />
            </DataGridComboBoxColumn.ElementStyle>

        </DataGridComboBoxColumn>

        <!--<DataGridTemplateColumn Header="Week Days" Width="*" x:Name="cmbTemplateColumn">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="cmbAdditionColume" Foreground="Black"
                              DisplayMemberPath="Spaltenname" Style="{StaticResource ComboBoxStyle1}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>-->

    </DataGrid.Columns>

    <DataGrid.Resources>
        <!-- Select everything with orange... -->
        <SolidColorBrush 
       Color="Transparent" 
       x:Key="{x:Static SystemColors.HighlightBrushKey}" />
        <!-- ...all the time -->
        <SolidColorBrush 
        Color="Transparent" 
        x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" />
        <!--Design kopfzeile-->
        <Style TargetType="{x:Type DataGridColumnHeader}" x:Name="test2" >
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="BorderBrush" Value="Green"/>
            <Setter Property="Padding" Value="10 0 0 0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <Grid x:Name="insideHeader" Background="Transparent">
                            <Border x:Name="borderHeader" BorderThickness="0"
                    CornerRadius="10" 
                    Background="White"
                    Padding="10,0,0,0"
                    >
                                <ContentPresenter />
                            </Border>
                            <!--<Thumb HorizontalAlignment="Right"
       Grid.Column="1"
       Name="PART_HeaderGripper"
       Margin="0,4,0,4"
       Width="2"/>-->
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter  Property="Foreground" Value="{DynamicResource PrimaryBlueColor}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="borderHeader" Property="Background" Value="{DynamicResource SecundaryGrayColor}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Content" Value=">" />
            <Setter Property="Foreground" Value="{DynamicResource PrimaryBlueColor}" />
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                        <Grid x:Name="insideHeader" Background="Transparent">
                            <Border x:Name="borderHeader" BorderThickness="0"
                    CornerRadius="2"
                    Background="White"
                    Padding="0,10,0,0"
                    >
                                <TextBlock Text=">" FontSize="12" VerticalAlignment="Top" Margin="0 0 0 0" TextAlignment="Center" FontWeight="Bold" Width="20" Foreground="{DynamicResource PrimaryBlueColor}" />

                            </Border>
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True" >
                                <Setter TargetName="borderHeader" Property="Background" Value="{DynamicResource PrimaryGrayColor}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="borderHeader" Property="Background" Value="{DynamicResource SecundaryGrayColor}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>

        <!--Deaktivieren Des rowheader
<Style TargetType="{x:Type DataGridRowHeader}">
    <Setter Property="Background" Value="Transparent"/>
</Style>-->

        <!--Cellen Design-->
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="FontFamily" Value="sans-serif"/>
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="BorderBrush" Value="#333333"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border x:Name="insideBorder" Background="Transparent" BorderThickness="0 2 0 1" BorderBrush="#dddddd">
                            <Border x:Name="BorderCell" BorderThickness="0"
                    CornerRadius="6"
                    Background="White"
                    Padding="8,5,0,5"
                    Margin="2">
                                <ContentPresenter/>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter  Property="Foreground" Value="{DynamicResource PrimaryBlueColor}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.Resources>
</DataGrid>

my itemtemplate of my ComboBox:

<Setter Property="ItemTemplate">
    <Setter.Value>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=cmbIsChecked, Mode=TwoWay}" Width="20"/>
                <TextBlock Text="{Binding Path=Spaltenname}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </Setter.Value>
</Setter>

my "bischenanders" collection (sry for that bad name I will fix that in the future)

my outputColumns class:

public class OutputColumns
{
    public string Spaltenname{ get; set; }
    public bool IsChecked { get; set; }

    public bool cmbIsChecked { get; set; }
}

thats the way I read the selected Items for the rows and for the Combobox rows

public List<OutputColumns> GetSelectedItems()
{
    var selectedItems = new List<OutputColumns>();

    foreach (OutputColumns item in View.bischenanders)
    {
        try
        {
            if (item.IsChecked)
            {
                selectedItems.Add(item);
            }
        }
        catch
        {

        }
    }
    return selectedItems;
}

public List<OutputColumns> cmbGetSelectedItems()
{

    var selectedItems = new List<OutputColumns>();

    foreach (OutputColumns item in View.bischenanders)
    {
        try
        {
            if (item.cmbIsChecked)
            {
                selectedItems.Add(item);
            }
        }
        catch
        {

        }
    }
    return selectedItems;
}

ut when I check in one CombobBox every combobox in the DataGrid has the same checked Values

It looks like that every time I open a new ComboBox he getting the data from the class and then there will the programm find a version where it is checked, because ComboBoxs I already have open get there own checked values, only the new ones getting the checked one that are checked in the previous checkbox.

Thanks for help!


Answers (1)