开发者

Loading UserControl to the region of the Window on ComboBox change

开发者 https://www.devze.com 2023-04-07 15:51 出处:网络
I have got ComboBox which is populated with collection of customTypes. On comboBox change, I woul开发者_开发问答d like to load/change content in the particular region so that it loads related data for

I have got ComboBox which is populated with collection of customTypes. On comboBox change, I woul开发者_开发问答d like to load/change content in the particular region so that it loads related data for the selected ComboBox item (it could be in the form of loading userControl or I i dont mind specifying DataTemplate for it).

It is similar to this question WPF Load Control question. But in this question, he is talking about individual DataTemplates in the actual Listbox, while I am talking about populating certain window region on ComboBox change.

I am using MVVM (and not PRISM) .Net 3.5.


U could use ContentControl which is the placeholder for the actual Content that is dynamically decided as per combobox selection.

The follwoing code is just for guidance

<Window ...>

   <Window.Resources>
       <MyView1 x:Key="MyView1" />
       <MyView2 x:Key="MyView2" />
   </Window.Resources>  

   ...

   <ComboBox x:Name="MyCombo">
       <ComboBox.ItemsSource>
           <sys:String>"MyView1"</sys:String>
           <sys:String>"MyView2"</sys:String>
           ....
       </ComboBox.ItemsSource>
   </ComboBox>

   ... 

   <!-- This is where your region is loaded -->
   <ContentControl>
       <ContentControl.Style>
           <Style TargetType="{x:Type ContentControl}">
               <Style.Triggers>
                   <DataTrigger Binding="{Binding Path=SelectedItem,
                                                  ElementName=MyCombo}"
                                Value="MyView1">
                        <Setter Property="Content"
                                Value="{StaticResource MyView1}"
                   </DataTrigger>
                   <DataTrigger Binding="{Binding Path=SelectedItem,
                                                  ElementName=MyCombo}"
                                Value="MyView2">
                        <Setter Property="Content"
                                Value="{StaticResource MyView2}"
                   </DataTrigger>
               </Style.Triggers>
           </Style>
       </ContentControl.Style>
   </ContentControl> 
</Window>

The data loading can be part of the MyView1 and MyView2 user control's constructor or your main UI's data context view model.


As far as I understand the question is how to change underlying data being bound to UI and not a DataTemplate only.

You can use EventSetter which will be handled in code behind where you can switch DataContext for a region you've mentioned:

<ComboBox>
     <ComboBox.Resources>         
         <Style TargetType="ComboBoxItem">             
           <EventSetter Event="Selector.SelectionChanged"
                        Handler="YourHandler"/>         
         </Style>     
     </ComboBox.Resources> 
</ComboBox>

But from MVVM perspectives it could be not perfect solution so you can introduce your own ComboBox class wich is Command aware, see this SO post: WPF command support in ComboBox

In this way you can decouple logic from UI using Command.

0

精彩评论

暂无评论...
验证码 换一张
取 消