首页 > 解决方案 > 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
        }
    }
}

所以我想询问解决这个问题的所有可能方法,以及哪种方法是完成它的最佳方法。非常感谢!

标签: c#wpfcombobox

解决方案


comboBox.ItemsSourcePropertyInfo项目组成,而不是颜色。您必须检索该属性的值才能用于画笔:

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>

推荐阅读