Layer Combobox tool

Layer Combobox tool

Amremad
Collaborator Collaborator
3,918 Views
12 Replies
Message 1 of 13

Layer Combobox tool

Amremad
Collaborator
Collaborator

Hello Every body :

 

i hope to fine any combox  tool like autocad layer combox tool .. to design my custom layer manger :: can i find it ??

 

0 Likes
Accepted solutions (1)
3,919 Views
12 Replies
Replies (12)
Message 2 of 13

fieldguy
Advisor
Advisor

That looks like a datagridview control with image and text columns.  I can't help with adapting it for the ribbon but there are plenty of posts here on that topic.   

Message 3 of 13

FRFR1426
Collaborator
Collaborator

If you can use WPF, there is a way to get a list of layers with their properties (Color, IsOff...) :

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows.Data;

namespace MyNameSpace
{
    public partial class LayerManagerWindow
    {
        public DataItemCollection Layers { get; set; }

        public LayerManagerWindow()
        {
            InitializeComponent();

            Layers = Application.UIBindings.Collections.Layers;

            DataContext = this;
        }
    }
}

<Window x:Class="MyNameSpace.LayerManagerWindow"
             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" 
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ComboBox ItemsSource="{Binding Layers}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Color}" />
                        <TextBlock Text="{Binding Name}" Margin="7,0,0,0" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 4 of 13

Amremad
Collaborator
Collaborator

sorry i didnt use wpf before .. and i didn't understand your answer 

 

so can you send me the code that working with this way and i will understand it after run it  🙂 

0 Likes
Message 5 of 13

FRFR1426
Collaborator
Collaborator

Here is a WinForms version:

 

DataItemCollection layers = Autodesk.AutoCAD.ApplicationServices.Application.UIBindings.Collections.Layers;
var bs = new BindingSource(layers, null);
comboBox1.DataSource = bs;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += (o, args) =>
{
ICustomTypeDescriptor dataItem = layers[args.Index];
PropertyDescriptorCollection properties = dataItem.GetProperties();
var color = (Autodesk.AutoCAD.Colors.Color) properties["Color"].GetValue(dataItem);
Rectangle bounds = args.Bounds;
int itemHeight = bounds.Height;
Graphics graphics = args.Graphics;
args.DrawBackground();
var rect = new Rectangle(bounds.Left + 1, bounds.Top + 1, itemHeight - 2, itemHeight - 2);
graphics.FillRectangle(new SolidBrush(color.ColorValue), rect);
graphics.DrawRectangle(new Pen(Color.Black), rect);
graphics.DrawString((string) properties["Name"].GetValue(dataItem), args.Font,
new SolidBrush(args.ForeColor), bounds.Left + itemHeight + 6, bounds.Top + 1);
args.DrawFocusRectangle();
};

2015-08-14_162608.png

 

Unfortunately, I haven't found a way to sort the layers by their name and I'm running out of time. You should iterate the layer table to construct your list and then sort it.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 6 of 13

FRFR1426
Collaborator
Collaborator

I cannot give up so easily, it's stronger than me:

 

DataItemCollection layers = Autodesk.AutoCAD.ApplicationServices.Application.UIBindings.Collections.Layers;
List<ICustomTypeDescriptor> list = layers.OfType<ICustomTypeDescriptor>().ToList();
list.Sort((a, b) => string.Compare(
    ((string) (a.GetProperties()["Name"].GetValue(a))), (string) (b.GetProperties()["Name"].GetValue(b)), StringComparison.CurrentCulture));
comboBox1.DataSource = list;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += (o, args) =>
{
    ICustomTypeDescriptor dataItem = list[args.Index];
    PropertyDescriptorCollection properties = dataItem.GetProperties();
    var color = (Autodesk.AutoCAD.Colors.Color) properties["Color"].GetValue(dataItem);
    Rectangle bounds = args.Bounds;
    int itemHeight = bounds.Height;
    Graphics graphics = args.Graphics;
    args.DrawBackground();
    var rect = new Rectangle(bounds.Left + 1, bounds.Top + 1, itemHeight - 2, itemHeight - 2);
    graphics.FillRectangle(new SolidBrush(color.ColorValue), rect);
    graphics.DrawRectangle(new Pen(Color.Black), rect);
    graphics.DrawString((string) properties["Name"].GetValue(dataItem), args.Font, 
        new SolidBrush(args.ForeColor), bounds.Left + itemHeight + 6, bounds.Top + 1);
    args.DrawFocusRectangle();
};

 

 

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 7 of 13

Amremad
Collaborator
Collaborator

thank you very much .. 

 

but i know this way ..   your combobox doesn't have click on every item like autocad combobox . 

 

i need when user click on color rectangle he can choose another color  like autocad combobox . i hope you understand me

 

so is WPF give what i want ???

0 Likes
Message 8 of 13

FRFR1426
Collaborator
Collaborator
Accepted solution

Yes, in WPF it's simpler to make this kind of custom controls. But I cannot explain WPF in a forum thread...

 

Here is an article on the WPF combobox: http://www.wpf-tutorial.com/list-controls/combobox-control/

 

You can also make your own control with a popup window like this: http://www.codeproject.com/Articles/27472/ColorComboBox

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 9 of 13

Amremad
Collaborator
Collaborator

thank you very much 

 

i'm reading now about wpf 

0 Likes
Message 10 of 13

FRFR1426
Collaborator
Collaborator
Just found the missing bit for the WinForms solution: http://stackoverflow.com/q/18521110/200443
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 11 of 13

Amremad
Collaborator
Collaborator

hi again 🙂

 

i was using  this line 

colorPicker1.SelectedIndex = colorPicker1.Items.IndexOf("Yellow");

before using the new combobox ..

 

no this code doesn't work becouse he search in item text , but the correct must searching in SelectedValue 

 

is there another easy way other than using loop ??

0 Likes
Message 12 of 13

Anonymous
Not applicable

hi,

 

I would be very happy if you could provide me coding for C# to display the layer and control the layer by windows form application

0 Likes
Message 13 of 13

Mahass
Participant
Participant

Thank you for this helpful code.

Please can you tell me how to show the layerManagerWindow in the ribbon?

Is it possible to do similar ribboncombo with specific elements (Color, checkbox,Text) for each combo item?

0 Likes