0

How to let the program know which category has been selected? If you will be able to attach code as well that would be really great!

Here's the of the source code and addin so you can have the idea and say if something should be changed immediately.

screenshot

Basically, main stuff is marked in red color. Many tutorials advice on using a string variable to keep category in, but I have no idea what am I supposed to do with it now or if it is a good solution at all? Thanks!

public class Class : IExternalCommand
{

    public Result Execute(ExternalCommandData commandData,
                          ref string message,
                          ElementSet elements)

    {
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Document doc = uidoc.Document;
        Settings documentSettings = doc.Settings;
        Categories categories = documentSettings.Categories;

        SortedList<string, Category> myCategories = new SortedList<string, Category>();

        foreach (Category c in categories)
        {
            myCategories.Add(c.Name, c);
        }

        myCategories.Clear();

        foreach (Category c in categories)
        {
            if (c.AllowsBoundParameters)
            myCategories.Add(c.Name, c);
        }
        UserWindow UserWindow = new UserWindow(myCategories);
        UserWindow.Show();

       return Result.Succeeded;

    }
}

 

UserWindow.xaml.cs

public partial class UserWindow : Window
{
    SortedList<string, Category> myCategories;

    public UserWindow(SortedList<string, Category> elements)
    {
        InitializeComponent();

        myCategories = elements;
        AllTheCategories.ItemsSource = myCategories;

        string ChosenCategory = AllTheCategories.SelectedItem.ToString()
    }
}

`

fysharp
  • 101
  • 7
  • 2
    Don't post source code as a screenshot! Post the code as **properly formatted text** directly in the question! – marc_s Apr 30 '21 at 05:54
  • Could you explain more specifically what you want? I will make you a sample. – jamesnet214 Apr 30 '21 at 06:44
  • Goal is to concatenate parameters of category's elements. 1)List the categories in the comboboxes and then display a list of available parameters in common for those categories (in case user selects more than one category). 2)The user selects the parameters, choose the order in which they are going to follow one another and run the concatenation command. Revit has Categories like Walls or Windows and they have parameters like "Height" or "Width" but some custom parameters can also be added. For example "Manufacturer Name".Thanks for providing help. I emailed you as well. @james.lee – fysharp Apr 30 '21 at 08:36
  • @marc_s just wanted you guys to see the whole picture on one image, every file of the program. – fysharp Apr 30 '21 at 08:39

2 Answers2

0

You are defining ChosenString as a variable inside the constructor of UserWindow. As soon as this constructor finishes, the variable will no longer exist.

With the little code you presented, and also as a screenshot (next time please upload it as properly formatted text), I would tell you to follow the pattern presented in here by Geoff Bennet Binding a WPF ComboBox to a custom list and it's corresponding answer.

What you want to achieve is to create a Data Binding between a public property (let's call it ChosenString) and the SelectedItem of your ComboBox. This Binding will make the property ChosenString change each time the SelectedItem changes in the ComboBox.

Miguel
  • 143
  • 1
  • 7
0

You are using a SortedList<string, Category>, which means its items are of the key-value type KeyValuePair<string, Category>. It has a Key property (string) and a Value property (Category). In order to display only the key - which is the category - you can specify the DisplayMemberPath.

Gets or sets a path to a value on the source object to serve as the visual representation of the object.

AllTheCategories.DisplayMemberPath = "Key";

To get the selection in code-behind (you do not seem to use MVVM), you can add an event handler to the SelectionChanged event that will get called whenever the selection changes. You can then access the SelectedItem, which again is a key-value pair, and use its Value property to get the Category.

public UserWindow(SortedList<string, Category> elements)
{
   InitializeComponent();

   AllTheCategories.ItemsSource = elements;
   AllTheCategories.DisplayMemberPath = "Key";
   AllTheCategories.SelectionChanged += OnSelectionChanged;
}

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (AllTheCategories.SelectedItem is null)
   {
      // ...nothing selected.
   }
   else if (AllTheCategories.SelectedItem is KeyValuePair<string, object> keyValuePair)
   {
      var category = keyValuePair.Value;
      // ...use the category.
   }
}

In an MVVM scenario, you would just bind the SelectedItem to a propety on your view model.

<ComboBox ItemsSource="{Binding MyElements}" SelectedItem="{Binding SelectedElement}"/>
thatguy
  • 21,059
  • 6
  • 30
  • 40