سؤال

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?

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top