按属性替换UserControl

时间:2013-03-18 08:34:56

标签: wpf mvvm binding user-controls

我有两个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并再次创建它。

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

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

1 个答案:

答案 0 :(得分:5)

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

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>

创建两个新的(空)UserControl并设置两种不同的颜色:

Off On

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

[编辑]

因此,如果您想知道创建的内容,请将一些消息框打到用户控件的ctor中

public UserControl1()
{
    InitializeComponent();

    MessageBox.Show("Created UserControl1");
}

public UserControl2()
{
    InitializeComponent();

    MessageBox.Show("Created UserControl2");
}

对我来说,它们都是在应用程序启动时创建的。即UserControl2尚未可见,但已创建。可以假设它在数据触发器切换之前没有约束;但是,如果您在用户控件中做一些代码隐藏,那可能会搞砸WPF。