0

I am a beginner at WPF so please excuse me if this question is too simple :)

I have a listbox which I would like to filter by various filter conditions. This listbox I fill with instances of a particular type. Each filter condition is associated with one of the listbox items' properties. (They are like: this or that string property starts with string XXX.)

For this I would need a menu for each property from which users can select the filter conditions they want to filter the items with. Each property of the same type will have the very same set of menu items with the various filter conditions. (For strings: starts with, ends with... for ints: lower than, higher than, etc.) The menus require some code behind too so I don't want to program these for each property separately.

My problem is that I don't know in what way could I program these. I cannot program them as UserControls because all what I need is MenuItems in a Menu. But I cannot program them as MenuItem derived classes because I would need the XAML for designing them for each type. Could I create a MenuItem derived class with a XAML somehow? Or do you have any other suggestions?

canahari
  • 512
  • 1
  • 4
  • 9
  • What do you mean a menu for each property? – paparazzo May 13 '14 at 14:34
  • Perhaps you need to create context menu *dynamically* (aka *in code*)? This can be a single method of some class (or even straight inside event handler, you didn't mentioned MVVM). – Sinatr May 13 '14 at 14:35
  • Blam: the class the items are from has some properties like: StringProperty1, StringProperty2, IntProperty1 etc. I have one menu for StringProperty1 (with submenus like StartsWith, EndsWith), one menu for StringProperty2 (exactly like the previous one), one menu for IntProperty1 (with submenus like LowerThan etc) ... and I do not want to program the menus separately for the various properties of the same type. – canahari May 13 '14 at 14:39
  • Sinatr: this is exactly what I do not want to do :) I need the xaml because the menus are not that simple at all. – canahari May 13 '14 at 14:40
  • So then is answer from Sheridan correct? "a menu for each property" "type will have the very same set of menu items" is still not clear to me – paparazzo May 13 '14 at 14:48
  • I have a class A with properties A.Prop1, A.Prop2, A.Prop3. I need a "main menu" like thing with the following texts: A.Prop1, A.Prop2, A.Prop3. If the user clicks on for example A.Prop1 he gets a submenu with which he can filter the listbox by various filter conditions associated with Prop1. The thing is, I want these submenus to be the same for all the strings, and I want another type of submenu for the ints etc. So one type of submenu for one CLR type. Basically I want to do the same thing as MS Excel's filters. I'm not sure the answer helps in this. – canahari May 13 '14 at 14:54
  • Then fix the question to be more clear. Not sure why you are putting filters into a Menu and MenuItem. – paparazzo May 13 '14 at 15:03

1 Answers1

1

In WPF, we work with data elements whose public properties are data bound to the properties of various UI controls via DataTemplates. Please see the Data Templating Overview page on MSDN for the full story.

In order to do this, we develop custom classes that contain all of the necessary properties that we need to display and then we declare one or more DataTemplates that define the binding connections between the classes and the UI controls, or MenuItems in your case.

The benefit of this is that in order to display a Menu in the UI, you just need to data bind one of your custom menu class objects to a control in the UI and let the DataTemplate do the rest. So if you want to change the menu contents, you just need to change the data item that is data bound to the Menu.

So to answer your question directly, a Menu control would be most suitable for you to use, but you don't store the Menu properties in your code behind... you store the property values in your custom classes that will be data bound to the Menu control properties:

<Menu ItemsSource="{Binding CollectionOfYourCustomClassItems}" ... />

It is worth pointing out that you may need to set the child MenuItem properties in a Style and not a DataTemplate as usual (taken from the accepted answer to the WPF MenuItem : Mix databound items and static content question (which I recommend that you read) here on Stack Overflow):

<MenuItem Header="_Recent Files" ItemsSource="{Binding Commands,Mode=OneWay}">
  <MenuItem.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
       <Setter Property="Header" Value="{Binding Path=ShortName}" />
       <Setter Property="ToolTip" Value="{Binding Path=FileName}" />
       <Setter Property="Command" Value="{Binding Path=OpenCommand}" />
       <Setter Property="CommandParameter" Value="{Binding Path=OpenParameter}" />
       <Style.Triggers>
         <DataTrigger Binding="{Binding Path=IsSeparator}" Value="true">
           <Setter Property="MenuItem.Template">
             <Setter.Value>
               <ControlTemplate TargetType="{x:Type MenuItem}">
                 <Separator Style="{DynamicResource {x:Static MenuItem.SeparatorStyleKey}}"/>
               </ControlTemplate>
             </Setter.Value>
           </Setter>
         </DataTrigger>
       </Style.Triggers>
     </Style>
   </MenuItem.ItemContainerStyle>
 </MenuItem>

You will find many more tutorials and related questions regarding data binding to MenuItems online, so I won't go over everything again here. Please see the following article to start with:

Binding menus using HeirarchicalDataTemplates

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I understand (on a basic level) about DataTemplates and data binding, but I'm not sure I understand the proposed solution. :) If I understand you correctly, my question was about how to develop that "YourCustomClass" type in "CollectionOfYourCustomClassItems". If that "YourCustomClass" would be a class that defines a filter menu for a particular type. It is important that I can't really develop these menus without the XAML because their inner structure is a bit difficult. – canahari May 13 '14 at 14:47
  • The properties that you would need to add into your custom class will depend on what functionality you require from your `MenuItem`s. For example, if you want the `MenuItem`s to do something when you click them, then you'll need to add an `ICommand` property into your class... logic states that the user would want to see some description, so you'd then need a `string` property to label your `MenuItem`s. It's as simple as that... you add whatever properties that you need and no more. Perhaps you should start using WPF with a simpler project until you understand how it works? – Sheridan May 13 '14 at 15:12
  • Would that I could, but this is the project I have now and I have to cope with it. I guess I understand what you propose, the problematic part is still that I would need the designer to edit my menus because they are not so simple, but datatemplates would need to be created by hand in the XAML. Is not that right? – canahari May 14 '14 at 12:31
  • That is not necessarily correct... `DataTemplate`s can also be generated using later version(s) of Visual Studio that include Blend. There are also third party utilities that provide similar functionality. – Sheridan May 14 '14 at 13:11