我的DataGrid包含3个数字参数和颜色的图形对象列表(抛物线)(类型为br
的{{1}}属性)。
我想从放置在SolidColorBrush
中的ComboBox中选择颜色,以便将所选类型(DataGridTemplateColumn
)的值保存在DataGrid中所选项目的SolidColorBrush
属性中。
图片显示了主窗口的工作原理:
创建新项目时,它会自动添加到DataGrid,但DataGrid中的br
列不会显示其颜色。在代码中以编程方式设置Color
值时,ComboBox也不会更新。
当我从下拉列表中手动选择颜色值时,它不会保存在所选项目的br
属性中。
在这种情况下,如何正确组织数据绑定?
我的简单图形元素类:
br
主窗口代码:
using System;
using System.Windows.Media;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfParabola
{
public class Parabola
{
public double a { get; set; } = 1;
public double b { get; set; } = 1;
public double c { get; set; } = 1;
public SolidColorBrush br { get; set; } = new SolidColorBrush(Colors.Black);
PropertyInfo[] brushes = typeof(Brushes).GetProperties();
public Parabola() { }
public Parabola(Random rnd)
{
a = (double)rnd.Next(2, 20) / 100;
b = (double)rnd.Next(2, 20) / 100;
c = (double)rnd.Next(1, 30);
int random = rnd.Next(brushes.Length);
br = (SolidColorBrush)brushes[random].GetValue(null, null);
}
public Parabola(double _a, double _b, double _c)
{
a = _a;
b = _b;
c = _c;
}
public double y(double x)
{
return Math.Pow(a * x, 2) + b * x + c;
}
}
}
及其背后的代码:
<Window x:Class="WpfParabola.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfParabola"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="725"
x:Name="_this">
<Window.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">
</ObjectDataProvider>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid Grid.Row="0"
Grid.Column="0"
Name="parabolasDataGrid"
CanUserAddRows="False"
ItemsSource="{Binding parabolas}">
<DataGrid.Columns>
<DataGridTextColumn Header="a" Width="20" Binding="{Binding a}"></DataGridTextColumn>
<DataGridTextColumn Header="b" Width="20" Binding="{Binding b}"></DataGridTextColumn>
<DataGridTextColumn Header="c" Width="20" Binding="{Binding c}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Color" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
SelectedItem ="{Binding br}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="50" Height="{Binding ElementName=FontSize+4}" Margin="2" Background="{Binding Name}"/>
<TextBlock TextAlignment="Left" VerticalAlignment="Center" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Canvas Grid.Row="0" Grid.Column="1" Name="parabolaCanvas"></Canvas>
<StackPanel Grid.Row="1" Grid.Column="1">
<Button Padding="3" Margin="3" Click="Button_Click">Add parabola</Button>
<Button Padding="3" Margin="3" IsCancel="True" Click="Button_Click_1">Close</Button>
</StackPanel>
</Grid>
答案 0 :(得分:1)
所以你有一组抛物线,你绑定你的集合,但你不能将你的ComboBox上的SelectedItem绑定到你的变量BR。您还需要使用NotifyPropertyChanged,以便在选择其他颜色时更新网格。