C# 从中绑定颜色
My DataGrid保存具有3个数值参数和颜色的图形对象(抛物线)列表(属性名为C# 从中绑定颜色,c#,wpf,combobox,datagrid,datagridtemplatecolumn,C#,Wpf,Combobox,Datagrid,Datagridtemplatecolumn,My DataGrid保存具有3个数值参数和颜色的图形对象(抛物线)列表(属性名为br,类型为SolidColorBrush) 我想从放置在DataGridTemplateColumn中的组合框中选择颜色,以便将类型(solidcolorbush)的选定值保存在DataGrid中选定项的名为br的属性中 此图显示了主窗口的工作方式: 创建新项时,它会自动添加到数据网格中,但数据网格中的Color列不会显示其颜色。在代码中以编程方式设置br值时,组合框也不会更新 当我从下拉列表中手动选择颜色值时
br
,类型为SolidColorBrush
)
我想从放置在DataGridTemplateColumn
中的组合框中选择颜色,以便将类型(solidcolorbush
)的选定值保存在DataGrid中选定项的名为br
的属性中
此图显示了主窗口的工作方式:
创建新项时,它会自动添加到数据网格中,但数据网格中的Color
列不会显示其颜色。在代码中以编程方式设置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>
System.Windows.Media.Color、PresentationCore、,
版本=3.0.0.0,区域性=中性,
PublicKeyToken=31bf3856ad364e35
加抛物线
关
及其背后的代码:
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfParabola
{
public partial class MainWindow : Window
{
Random rnd = new Random();
public ObservableCollection<Parabola> parabolas { get; set; } = new ObservableCollection<Parabola>();
public MainWindow()
{
InitializeComponent();
DataContext = this;
parabolasDataGrid.AutoGenerateColumns = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Path path = new Path();
PathGeometry geometry = new PathGeometry();
PathFigure figure = new PathFigure();
Parabola p = new Parabola(rnd);
parabolas.Add(p);
figure.StartPoint = new Point(1, p.y(1));
for(double x=2; x<300; x += 0.5)
{
figure.Segments.Add(new LineSegment()
{
Point = new Point(x, p.y(x))
});
}
path.Stroke = p.br;
path.StrokeThickness = 2;
geometry.Figures.Add(figure);
path.Data = geometry;
parabolaCanvas.Children.Add(path);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Close();
}
}
}
使用系统;
运用系统反思;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统组件模型;
使用System.Collections.ObjectModel;
命名空间WpfParabola
{
公共部分类主窗口:窗口
{
随机rnd=新随机();
公共ObservableCollection抛物线{get;set;}=new ObservableCollection();
公共主窗口()
{
初始化组件();
DataContext=this;
parabolasDataGrid.AutoGenerateColumns=假;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
路径路径=新路径();
PathGeometry=新的PathGeometry();
PathFigure=新的PathFigure();
抛物线p=新抛物线(rnd);
抛物线加法(p);
figure.StartPoint=新点(1,p.y(1));
对于(double x=2;x,您有一个抛物线集合,可以将集合绑定到该集合,但不会将组合框上的SelectedItem绑定到变量BR。您还需要使用NotifyPropertyChanged,以便在选择其他颜色时更新网格。我添加了SelectedItem=“{binding BR}”
,对吗?但是我的抛物线类中的属性br
无论如何都没有更新。可能问题出在类型转换中。ComboBox中填充了静态资源colorPropertiesOdp
,它是PropertyInfo
集合,而不是笔刷集合。如何处理?这将是一个问题,你知道吗可以使所选项目成为colorPropertiesOdp的任何类型,当属性更改时,更新您的br属性。但是,我将创建一个具有所需属性的实际类型,而不是propertyInfo集合,并将comobBox绑定到该类型。