Cad二次开发显示图层

要在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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周杰伦fans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值