Combobox selection resets selection in another combobox

Tuesday, July 8, 2014

I am having an issue with using two comboboxes within the same row of a listview. I am using one combobox to select the background colour of a textblock, and the other to select the fontcolour. However, when i make a selection in one combobox, the selection is reset in the other, but the underlying dataobject manintains its value. I am using a custom user control for the colour picker.


Main.xaml



<GridView>
<GridViewColumn Header="ID" Width="40" DisplayMemberBinding="{Binding ID}"/>

<GridViewColumn Header="Background Colour" Width="160">
<GridViewColumn.CellTemplate>
<DataTemplate>
<res:ColorPicker x:Name="backgroundColourPicker"
SelectedColor="{Binding BackgroundColour,
Converter={StaticResource ColorToSolidColorBrush_ValueConverter} ,
Mode=TwoWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

<GridViewColumn Header="Text Colour" Width="160">
<GridViewColumn.CellTemplate>
<DataTemplate>
<res:ColorPicker x:Name="textColourPicker"
SelectedColor="{Binding TextColour,
Converter={StaticResource ColorToSolidColorBrush_ValueConverter} ,
Mode=TwoWay}"
/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

<GridViewColumn Header="Description">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}"
Background="{Binding BackgroundColour, Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"
Foreground="{Binding TextColour, Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>


ColorPicker.xaml



<UserControl x:Class="TyphoonSupportSystems.ESS.XCS.HTC.Resources.ColorPicker"
xmlns="http://ift.tt/o66D3f"
xmlns:x="http://ift.tt/mPTqtT"
xmlns:mc="http://ift.tt/pzd6Lm"
xmlns:d="http://ift.tt/pHvyf2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Name="uccolorpicker">

<UserControl.Resources>
<ResourceDictionary>
<ObjectDataProvider MethodName="GetType" ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
<ObjectDataProvider.MethodParameters>
<sys:String>System.Windows.Media.Colors, PresentationCore,Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
</ResourceDictionary>
</UserControl.Resources>

<Grid>
<ComboBox Name="superCombo"
ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Name"
SelectedValue="{Binding ElementName=uccolorpicker, Path=SelectedColor}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="20" Height="20" Margin="5" Background="{Binding Name}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>


ColorPicker.xaml.cs



public partial class ColorPicker : UserControl
{
public ColorPicker()
{
InitializeComponent();
}

public Brush SelectedColor
{
get { return (Brush)GetValue(SelectedColorProperty); }
set
{
SetValue(SelectedColorProperty, value);
}
}

// Using a DependencyProperty as the backing store for SelectedColor.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor",
typeof(Brush), typeof(ColorPicker), new PropertyMetadata(null));

}


When i selected dark blue as a font colour, the background colour combobox is reset. Please advise how i can stop the colorpicker combobox losing its selectiuon when selecting a value in another on the same gridview row.


I have already tried setting the issynchronisedwithcurrentitem property on the colorpicker, however this stops my text from being updated.


Many thanks







http://ift.tt/1xLbeZa