gpt4 book ai didi

wpf - 按属性替换 UserControl

转载 作者:行者123 更新时间:2023-12-04 19:10:44 24 4
gpt4 key购买 nike

我有两个 User Control's坐在主要UserCotrol .

只有其中一个应该是主要的。

当您更改 ViewModel 中的某些属性时,我像这样改变它们:

<UserControl>
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Content">
<Setter.Value>
<local:UserControl1/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding IsTwo}" Value="True">
<Setter Property="Content">
<Setter.Value>
<local:UserControl2/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</UserControl>

当我将属性更改为 False它转到 UserControl1没有问题,但是当我将其更改为 True ,显示有问题,只有当我在适合的屏幕之间移动时,作为临时解决方案,我每次 UserControl 应该从 1 更改为 2 时创建事件。当该事件运行时,我删除主要 UserControl并再次创建它。

但是我的问题是为什么当我更改为一个时我不需要重新创建,而当我更改为两个时我可能需要?

不要给我任何解决这个问题的想法(我自己做过),我想解释为什么会这样,这是我感兴趣的。

最佳答案

你的WPF很好;我无法重现您的问题;启动一个新的 WPF 应用程序并将其转储到其中:然后替换为您的用户控件(请记住,如果您希望它绑定(bind)到特定的东西,您可能需要在模板中绑定(bind)用户控件)

ViewModel.cs:

public class ViewModel : INotifyPropertyChanged
{
private bool _isTwo;

public bool IsTwo
{
get { return _isTwo; }
set
{
_isTwo = value;
OnPropertyChanged("IsTwo");
}
}

public ICommand Switch
{
get { return new RelayCommand((_) => { IsTwo = !IsTwo; }, (_) => true); }
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}

public class RelayCommand : ICommand
{
private readonly Action<object> _action;
private readonly Func<object, bool> _predicate;

public RelayCommand(Action<object> action, Func<object, bool> predicate)
{
_action = action;
_predicate = predicate;
}

public bool CanExecute(object parameter)
{
return _predicate(parameter);
}

public void Execute(object parameter)
{
_action(parameter);
}

public event EventHandler CanExecuteChanged;
}

主窗口
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>

<DockPanel>
<Button DockPanel.Dock="Top" Command="{Binding Switch}"> Switch </Button>
<UserControl>
<ContentControl >
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Content">
<Setter.Value>
<local:UserControl1 DataContext="{Binding UserControl1ViewModel}"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding IsTwo}" Value="True">
<Setter Property="Content">
<Setter.Value>
<local:UserControl2/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</UserControl>
</DockPanel>

创建两个新的(空的)用户控件并设置两种不同的颜色:

Off
On

用您遇到问题的用户 Controller 替换其中之一。如果您有布局问题,您的用户控件很可能正在尝试调整自己的大小,或者您需要将它们放入更好的容器中。我将以上内容粘贴到 DockPanel 中。这通常非常擅长将控件扩展到可用空间。

[编辑]

因此,如果您想知道何时创建了什么,请将这些消息框添加到您的用户控件的 ctor 中
public UserControl1()
{
InitializeComponent();

MessageBox.Show("Created UserControl1");
}

public UserControl2()
{
InitializeComponent();

MessageBox.Show("Created UserControl2");
}

对我来说,它们都是在应用程序启动时创建的。即 UserControl2 尚不可见,但已创建。有人会认为它在数据触发器切换之前不会被绑定(bind);但是如果您在用户控件中执行一些代码隐藏,则可能会搞砸 WPF。

关于wpf - 按属性替换 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15472512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com