是否可以仅在WPF中使用触发器加载和卸载usercontrol
我正在尝试创建MDI类型的功能,以便加载与用户单击的按钮对应的用户控件,并卸载其余的控件。每个按钮都与一个userControl关联是否可以仅在WPF中使用触发器加载和卸载usercontrol,wpf,Wpf,我正在尝试创建MDI类型的功能,以便加载与用户单击的按钮对应的用户控件,并卸载其余的控件。每个按钮都与一个userControl关联 <Button Content="Worker registration"/> //UserControl1 <Button Content="Worker recognition"/> //UserControl2 ...and so on <Grid x:Name="UserControlManager"/> //用
<Button Content="Worker registration"/> //UserControl1
<Button Content="Worker recognition"/> //UserControl2 ...and so on
<Grid x:Name="UserControlManager"/>
//用户控制1
//UserControl2…等等
有没有不使用tabcontrol的理由?像这样
<TabControl>
<TabItem Header="Control A">
<local:ControlA/>
</TabItem>
<TabItem Header="Control B">
<local:UserControlB/>
</TabItem>
</TabControl>
或者使用ItemsSource绑定所有项目
<TabControl ItemsSource="{Binding MyItems}"/>
还有第三方TabControls,这非常好,就像devcomponents提供的一样。
如果TabControl不够用(我知道有很多问题),可以使用将某些属性转换为视图的。您可以使用中介和/或ViewModelLocator,我喜欢Galasoft的MVVM灯光。他们通过nuget提供一切,甚至为您设置一切:)
为按钮添加一个命令,用于选择要显示的内容。并添加用于显示SelectedControl的xaml
错误的中介程序/ViewmodelLocator;)改用Galasofts
公共类ViewModelLocator:INotifyPropertyChanged
{
私有用户控件selectedControl;
私有ObservableCollection控件=新ObservableCollection();
公共用户控件SelectedControl
{
获取{return selectedControl;}
设置
{
if(等于(selectedControl,value))返回;
selectedControl=值;
OnPropertyChanged();
}
}
公共可观测收集控制
{
获取{返回控件;}
设置
{
if(等于(控制,值))返回;
控件=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
希望有帮助
干杯
Stian有没有不使用tabcontrol的理由?像这样
<TabControl>
<TabItem Header="Control A">
<local:ControlA/>
</TabItem>
<TabItem Header="Control B">
<local:UserControlB/>
</TabItem>
</TabControl>
或者使用ItemsSource绑定所有项目
<TabControl ItemsSource="{Binding MyItems}"/>
还有第三方TabControls,这非常好,就像devcomponents提供的一样。
如果TabControl不够用(我知道有很多问题),可以使用将某些属性转换为视图的。您可以使用中介和/或ViewModelLocator,我喜欢Galasoft的MVVM灯光。他们通过nuget提供一切,甚至为您设置一切:)
为按钮添加一个命令,用于选择要显示的内容。并添加用于显示SelectedControl的xaml
错误的中介程序/ViewmodelLocator;)改用Galasofts
公共类ViewModelLocator:INotifyPropertyChanged
{
私有用户控件selectedControl;
私有ObservableCollection控件=新ObservableCollection();
公共用户控件SelectedControl
{
获取{return selectedControl;}
设置
{
if(等于(selectedControl,value))返回;
selectedControl=值;
OnPropertyChanged();
}
}
公共可观测收集控制
{
获取{返回控件;}
设置
{
if(等于(控制,值))返回;
控件=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
希望有帮助
干杯
Stian您可以使用数据模板加载视图,具体取决于您设置的数据(viweModel)
然后有一个ContentControl,您的内容将显示在其中
<Grid >
<ContentControl Content="{Binding MyContent}" />
</Grid
根据设置的数据(viweModel),可以使用DataTemplates加载视图
然后有一个ContentControl,您的内容将显示在其中
<Grid >
<ContentControl Content="{Binding MyContent}" />
</Grid
我不想在编译时加载所有用户控件,因为有许多元素是从数据库绑定的。那个么,即使用户可能永远不会打开它们,我为什么还要加载它们呢。早些时候,我使用了TabControl,因此将其删除。另外,我想在UIT方面有更多的灵活性,虽然我现在没有使用ViewModel,但我喜欢您的解决方案。但你给了我一个方法:)+1。我仍在考虑可能的解决方案。啊,动态xaml,那么你不能使用UserControl,你必须从网格(codebehind)继承。除此之外,请看一下如何使用中介或pref.a。我不想在编译时加载所有用户控件,因为有许多元素是从数据库绑定的。那个么,即使用户可能永远不会打开它们,我为什么还要加载它们呢。早些时候,我使用了TabControl,因此将其删除。另外,我想在UIT方面有更多的灵活性,虽然我现在没有使用ViewModel,但我喜欢您的解决方案。但你给了我一个方法:)+1。我仍在考虑可能的解决方案。啊,动态xaml,那么你不能使用UserControl,你必须从网格(codebehind)继承。撇开这一点,我们来看看如何使用中介或pref.a。
public NavigationEnum SelectedNavigationEnum
{
...
set
{
...
Navigate(value);
}
}
protected void Navigate(NavigationEnum part)
{
switch (part)
{
case NavigationEnum.EnumValue1:
ShowView1();
break;
case NavigationEnum.EnumValue2:
ShowView2();
...
}
}
private void ShowView1()
{
ViewModel1 viewModel = ObjectFactory.GetInstance<ViewModel1>();
MyContent = viewModel;
}