使用各种ViewModel重用TabControl中的UserControl(Reuse UserControl in TabControl with Various ViewModels)

编程入门 行业动态 更新时间:2024-10-08 02:25:21
使用各种ViewModel重用TabControl中的UserControl(Reuse UserControl in TabControl with Various ViewModels)

我有一个用户控件,我想重用多个ViewModel,它们都实现了相同的接口。 我想将这些嵌入到TabControl中。

目前我可以为单个实例执行此操作,但我正在努力重用我的UserControl。 对于单个实例,我可以在UserContol的xaml中绑定ViewModel或在后面的代码中实例化它,但是我无法弄清楚如何从更高级别设置它。

这是我的,

<TabControl HorizontalAlignment="Left" Height="800" Margin="0,0,0,0" VerticalAlignment="Top" Width="600"> <TabItem Header="Tab1"> <Frame Source="SomeUserControl.xaml" BorderThickness="0" Margin="0" /> </TabItem> </TabControl>

这是我想要实现的伪代码,

<TabControl> <TabItem Header="Tab1"> <Frame Source="{SomeUserControl.xaml, DataContext=ViewModel1}" /> </TabItem> <TabItem Header="Tab2"> <Frame Source="{SomeUserControl.xaml, DataContext=ViewModel2}" /> </TabItem> </TabControl>

谢谢!

I have one user control that I would like to reuse with multiple ViewModels, all which implement the same interface. I would like to have these embedded in a TabControl.

Currently I can do this for a single instance but I am struggling to reuse my UserControl. For the single instance I can either bind the ViewModel in the UserContol's xaml or instantiate it in the code behind, however I can't figure out how to set this from a higher level.

Here is what I have,

<TabControl HorizontalAlignment="Left" Height="800" Margin="0,0,0,0" VerticalAlignment="Top" Width="600"> <TabItem Header="Tab1"> <Frame Source="SomeUserControl.xaml" BorderThickness="0" Margin="0" /> </TabItem> </TabControl>

Here is pseudo code for what I would like to achieve,

<TabControl> <TabItem Header="Tab1"> <Frame Source="{SomeUserControl.xaml, DataContext=ViewModel1}" /> </TabItem> <TabItem Header="Tab2"> <Frame Source="{SomeUserControl.xaml, DataContext=ViewModel2}" /> </TabItem> </TabControl>

Thanks!

最满意答案

而不是硬编码选项卡,绑定到“item”类的ObservableCollection。 就是这样的

class MyTabItems : INotifyPropertyChanged { public string Header... public object DataContext... }

创建一个:

ObservableCollection<MyTabItems>

将TabControl ItemsSource绑定到ObservableCollection。 在TabControl的项模板中,将Header绑定到Header属性,将DataContext绑定到DataContext属性。 源可以在模板中进行硬编码,或者您可以向项类添加另一个属性,并在需要更多灵活性时将其绑定到该类。

Instead of hard coding the tabs, bind to an ObservableCollection of an "item" class. I.e. something like

class MyTabItems : INotifyPropertyChanged { public string Header... public object DataContext... }

create an:

ObservableCollection<MyTabItems>

Bind the TabControl ItemsSource to the ObservableCollection. In the item template for the TabControl bind the Header to the Header property and the DataContext to the DataContext property. The source can be hardcoded in the template or you can add another property to your item class and bind it to that if you want more flexibility.

更多推荐

ViewModel,xaml,实例,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-07-30 14:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TabControl   ViewModel   UserControl   ViewModels   Reuse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!