c# - WPF 根据使用 DataTemplate 的 ComboBox 选择更改其他控件属性
问题描述
我正在学习 WPF,我制作了一个简单的程序来更好地理解一些事情,但我被卡住了。在这个程序中,我想选择ComboBox中的颜色,并根据选择改变它上面TextBlock的前景色。我已经尝试过数据绑定,但它不起作用(我相信这是因为我将 DataTemplate 用于 ComboBox),所以我尝试通过事件处理程序来完成它,但我不太了解 SelectedItem 属性的工作原理。这是 XAML 代码:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="My First GUI" Height="450" Width="800">
<StackPanel Orientation="Vertical">
<TextBlock Text="Sample Text" FontSize="50" FontWeight="Bold" Name="title" HorizontalAlignment="Center" Margin="30"/>
<ComboBox Name="comboBox" SelectionChanged="comboBox_SelectionChanged" HorizontalAlignment="Center" Width="300">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Name="rect" Fill="{Binding Name}" Width="24" Height="24"/>
<TextBlock Text="{Binding Name, StringFormat={} {0}}" FontSize="20"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Window>
这里是后面的代码:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Combobox data setup
comboBox.ItemsSource = typeof(Colors).GetProperties();
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
title.Foreground = new SolidColorBrush(comboBox.SelectedItem as Color);//This line doesn't work
}
}
}
所以我想询问解决这个问题的所有可能方法,以及哪种方法是完成它的最佳方法。非常感谢!
解决方案
comboBox.ItemsSource
由PropertyInfo
项目组成,而不是颜色。您必须检索该属性的值才能用于画笔:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var pi = comboBox.SelectedItem as PropertyInfo;
if (pi != null)
{
var color = pi.GetValue(null) as Color;
if (color != null)
{
title.Foreground = new SolidColorBrush(color);
}
}
}
您还可以使用Brushes类:
public MainWindow()
{
InitializeComponent();
//Combobox data setup
comboBox.ItemsSource = typeof(Brushes).GetProperties();
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var pi = comboBox.SelectedItem as PropertyInfo;
if (pi != null)
{
title.Foreground = pi.GetValue(null) as Brush;
}
}
要通过绑定使用由名称组成的 ItemsSource 来获得相同的结果
comboBox.ItemsSource = typeof(Colors).GetProperties().Select(p => p.Name).ToList();
<TextBlock Name="title" Text="Sample Text"
Foreground="{Binding ElementName=comboBox, Path=SelectedItem}"
FontSize="50" FontWeight="Bold"
HorizontalAlignment="Center" Margin="30"/>
<ComboBox Name="comboBox" SelectedIndex="0" HorizontalAlignment="Center" Width="300">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Name="rect" Fill="{Binding}" Width="24" Height="24"/>
<TextBlock Text="{Binding StringFormat={} {0}}" FontSize="20"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
推荐阅读
- python - 如何在加载大型库时加速 Pytest 启动(太慢)
- angular - Angular - 测试/模拟使用 rxjs fromEvent 的订阅
- c++ - 复制构造函数在复制 std::vector 时工作得更快
比 std::copy - ios - 有没有办法下载 USDZ 并将其放置在现实套件场景中?
- javascript - SonarQube - 使用 sonar-project.properties 覆盖质量配置文件
- flutter - 颤振:预期类型错误的值。传入字符串,小部件期待别的东西
- elasticsearch - elasticsearch 通过减去两个数字字段进行排序
- objective-c - 在 Objective-c 中使用 Earlgrey 的 IOS Programmatic Tap
- firebase - 使用 Firebase 消息打开应用时 Flutter 显示通知
- javascript - Cookies 在 HTTP 页面之间工作,而不是在 HTTPS 页面之间工作