要在WPF中实现当选择ComboBox中的不同颜色时,更新颜色显示的功能,可以使用ComboBox的SelectedValue属性和绑定到颜色属性的TextBlock元素。
以下是示例代码,用于实现此功能:
<DockPanel>
<TextBlock Background="{Binding ElementName=layers, Path=SelectedValue}" Width="13" Height="13"/>
<ComboBox Name="layers" ItemsSource="{Binding Model.MyLayerNames}"/>
</DockPanel>
在这个示例中,我们首先创建一个TextBlock元素,它们都放在一个DockPanel中。TextBlock元素的Background属性绑定到ComboBox元素的SelectedValue属性,这意味着当ComboBox中选择的颜色更改时,TextBlock的背景颜色也会更改。ComboBox元素的ItemsSource属性绑定到ViewModel中的MyLayerNames属性,用于显示可供选择的颜色名称。
在ViewModel中,我们需要为MyLayerNames属性提供一个属性访问器,并且需要实现INotifyPropertyChanged接口,以便在属性更改时通知WPF框架。以下是示例代码:
private List<string> _myLayerNames = new List<string> {"Red", "Green", "Blue"};
public List<string> MyLayerNames
{
get { return _myLayerNames; }
set
{
if (_myLayerNames != value)
{
_myLayerNames = value;
OnPropertyChanged(nameof(MyLayerNames));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
通过块表拿到所有图层
public static List<string> GetLayerName()
{
List<string> layers = new List<string>();
using (Database db = Application.DocumentManager.MdiActiveDocument.Database)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
using (LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead))
{
foreach (var id in lt)
{
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(id, OpenMode.ForRead);
layers.Add(ltr.Name);
}
}
trans.Commit();
}
}
return layers;
}
> List<string> MyLayerNames = LayerHelper.GetLayerName();
要实现ComboBox选择时同步更新TextBlock的颜色,可以将TextBlock的背景色属性绑定到ComboBox的SelectedItem属性,然后使用一个值转换器(ValueConverter)来将选定的颜色名称转换为对应的颜色值。
<DockPanel>
<TextBlock Width="13" Height="13" Background="{Binding ElementName=layers, Path=SelectedItem, Converter={StaticResource ColorNameToBrushConverter}}"/>
<ComboBox Name="layers" ItemsSource="{Binding MyLayerNames}"/>
</DockPanel>
在这个示例中,我们使用了一个值转换器ColorNameToBrushConverter,用于将ComboBox的SelectedItem属性中的颜色名称转换为对应的Brush颜色值。这个值转换器的实现如下:
public class ColorNameToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return Brushes.Transparent;
}
else
{
string colorName = (string)value;
return GetColorCode();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public static class LayerHelper
{
public static string GetColorCode(this string colorString)
{
switch (colorString)
{
case "红":
return "#FF0000";
case "黄":
return "#FFFF00";
case "蓝":
return "#0000FF";
case "绿":
return "#008000";
default:
return "#000000";
//.................
}
}
}
这个值转换器实现了IValueConverter接口,可以将ComboBox中的选定的颜色名称转换为对应的Brush颜色值。如果ComboBox中没有选定任何颜色,则返回透明的Brush。在XAML中使用这个值转换器,我们需要在Window或UserControl的Resources中声明一个静态资源。
<Window.Resources>
<local:ColorNameToBrushConverter x:Key="ColorNameToBrushConverter"/>
</Window.Resources>
完整代码:
<Window x:Class="View.CadView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:View"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Height="300" Width="300"
d:DataContext="{d:DesignInstance Type=local:CadViewModel}">
<Window.Resources>
<local:ColorNameToBrushConverter x:Key="ColorNameToBrushConverter"/>
</Window.Resources>
<UniformGrid Background="White">
<GroupBox Header="图层:">
<StackPanel>
<DockPanel>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center"></CheckBox>
<TextBlock Background="{Binding ElementName=layers,Path=SelectedValue,Converter={StaticResource ColorNameToBrushConverter}}" Width="13" Height="13" ></TextBlock>
<ComboBox Name="layers" ItemsSource="{Binding MyLayerNames}"></ComboBox>
</DockPanel>
</StackPanel>
</GroupBox>
</UniformGrid>
</Window>