I'm struggling with a small Problem:
I've built a ComboBox which is a littlebit Special:
<ComboBox x:Name="selBenutzer" SelectionChanged="selBenutzer_SelectionChanged" Grid.Row="1" Height="30" Width="150" IsEditable="True" IsReadOnly="True" Text="Mitarbeiter" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="StackPanelSelBenutzer" Orientation="Horizontal" MouseLeftButtonDown="SelBenutzer_MouseLeftButtonDown">
<CheckBox Name="CheckBoxSelBenutzer" IsChecked="{Binding IsSelected}" Width="20" Checked="CheckBoxSelBenutzer_Checked" Unchecked="CheckBoxSelBenutzer_Unchecked"/>
<TextBlock Name="TextBlockSelBenutzer" Text="{Binding Value}" Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I needed a ComboBox for some Employees. But i needed to have CheckBoxes inside my Combobox (next to the TextBlock-UI for displaying the Item Name), so i could make a Multiselect. The Checked-/Unchecked-Events get the Text Property of the Checked-/Unchecked-Item and add it to a List. Then the List will be shown Comma-Separated in The .Text Propertie of the ComboBox..
private void CheckBoxSelBenutzer_Checked(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
StackPanel sp = (StackPanel)cb.Parent;
TextBlock tb = (TextBlock)sp.Children[1];
checkedUsersList.Add(tb.Text); // Unckecked: .Remove(tb.Text)
var result = String.Join(", ", checkedUsersList.ToArray());
selBenutzer.Text = result.ToString();
}
So this works very well.. But at then i got a Problem: What if you not hit the CheckBox? If i hit the TextBlock-UI instead of the Checkbox it not shows the Values I wanted to show and also not hit the CheckBox..
I got the Key and Value Pair from the Collection which was used as Source. So I've built a LeftMouseClickEvent on my StackPanel:
private void SelBenutzer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
StackPanel sp = (StackPanel)sender;
CheckBox cb = (CheckBox)sp.Children[0];
if (cb.IsChecked == true)
{
cb.IsChecked = false;
}
else
{
cb.IsChecked = true;
}
}
So now I was able to Klick on the TextBlock and the Checked-Property was updated automatically because of my MouseLeftButtonClick-Event.
But now when i just click on the TextBlock it first shows me the Value + Key -Pair and if i open the ComboBox again and Select another TextBlock the ComboBox.Text-Propertie is like an empty String?
I tried many Events but i think one last Event is always fired by the ComboBox automatically. But i don't know why and how it has changed?
I would be glad about every help
Aucun commentaire:
Enregistrer un commentaire