Reputation: 91
I have a ListView in WPF with text and colored rectangles. Additionaly there is a textbox and a rectangle with binding to the ListView. The selected Item of the Listbox is displayed in the textbox and the rectangle:
<ListView x:Name="StatusOEMList" HorizontalAlignment="Left" Height="195" Margin="50,50,0,0" VerticalAlignment="Top" Width="255" ItemsSource="{Binding OEM}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Status" DisplayMemberBinding="{Binding value}" />
<GridViewColumn Header="Farbe">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Rectangle Width="10" Height="10" Fill="{Binding color, Converter={StaticResource ColorToBrushConverter}}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<TextBox HorizontalAlignment="Left" Height="23" Margin="50,272,0,0" TextWrapping="Wrap" Text="{Binding ElementName=StatusOEMList, Path=SelectedItem.value}" VerticalAlignment="Top" Width="120"/>
<Rectangle Fill="{Binding ElementName=StatusOEMList, Path=SelectedItem.color, Converter={StaticResource ColorToBrushConverter}}" HorizontalAlignment="Left" Height="23" Margin="192,272,0,0" Stroke="Black" VerticalAlignment="Top" Width="23" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" Cursor="Pen"/>
When I change the text, the change is transfered back to the ListView. Nice. But how can I change the color of the rectangle and transfer the change back to the ListView? I implemented a ColorPicker to change the color fo the rectangle:
void rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.SelectedColor = ((SolidColorBrush)((Rectangle)sender).Fill).Color;
colorDialog.Owner = this;
if ((bool)colorDialog.ShowDialog())
{
((Rectangle)sender).Fill = new SolidColorBrush(colorDialog.SelectedColor);
}
}
I think I am overwriting the Binding in the Rectangle.Fill there. How can I just change the color and keep the Binding?
Upvotes: 0
Views: 324
Reputation: 35720
You are indeed overwriting Rectangle.Fill binding with local value. Try changing the bound property instead:
void rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.SelectedColor = ((SolidColorBrush)((Rectangle)sender).Fill).Color;
colorDialog.Owner = this;
if ((bool)colorDialog.ShowDialog())
{
var vm = StatusOEMList.SelectedItem as MyViewModel;
vm.color = colorDialog.SelectedColor;
}
}
Upvotes: 1