wpf - ViewModel 绑定(bind)错误

标签 wpf mvvm mvvm-light

我遇到错误。这是场景:

  • wpf 应用程序加载并且 ScrollViewer 的内容绑定(bind)到 ViewModel 中称为 ActiveFunction 的属性(其类型为 UserControl)。在该 ViewModel 的构造函数中为该属性设置了一个自定义控件 (UserCtrl1)。
  • 按下一个按钮,该按钮发出一个将 ActiveFunction 属性设置为新 UserControl (UserCtrl2) 的命令。即 this.ActiveFunction = new UserCtrl2();
  • 新的 UserControl 作为 ScrollViewer 的内容加载。一切似乎都很好。
  • 然后,按下一个按钮,该按钮发出一个命令,将 ActiveFunction 属性设置回原始 UserControl (this.ActiveFunction = new UserCtrl1();)。
  • 此时抛出异常——“指定元素已经是另一个元素的逻辑子元素,先断开连接。”

  • 谁能帮我解决这个问题。如果有帮助(它不是那么大),我很乐意上传整个 VS 解决方案。我真的很想了解这项技术的陷阱。我现在似乎在与技术作斗争,而不是利用它的力量。

    干杯

    最佳答案

    我对您描述的场景进行了最简单的实现,它对我来说没有错误。我将发布它的代码。请指出您的代码中的差异所在。

    <Window x:Class="LogicalChildException.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
                <Button Name="ChangeUserControl" Click="ChangeUserControl_Click">Change UserControl</Button>
            </StackPanel>
            <ScrollViewer Content="{Binding ActiveFunction}">
    
            </ScrollViewer>
        </DockPanel>
    </Window>
    
    <UserControl x:Class="LogicalChildException.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <TextBlock Height="500" FontSize="30">
                UserControl One
            </TextBlock> 
        </Grid>
    </UserControl>
    
    <UserControl x:Class="LogicalChildException.UserControl2"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <TextBlock Height="300" FontSize="10">
                UserControl Two
            </TextBlock>
    
        </Grid>
    </UserControl>
    
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace LogicalChildException
    {
    
        public partial class MainWindow : Window,INotifyPropertyChanged
        {
            public MainWindow()
            {
                InitializeComponent();
                ActiveFunction = new UserControl1();
                DataContext = this;
            }
    
            private void ChangeUserControl_Click(object sender, RoutedEventArgs e)
            {
                if (ActiveFunction is UserControl1)
                    ActiveFunction = new UserControl2();
                else
                    ActiveFunction = new UserControl1();
            }
    
            private UserControl _activeFunction;
            public UserControl ActiveFunction
            {
                get { return _activeFunction; }
                set
                {
                    _activeFunction = value;
                    if(PropertyChanged!=null)
                        PropertyChanged(this,new PropertyChangedEventArgs("ActiveFunction"));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
    

    关于wpf - ViewModel 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637978/

    相关文章:

    wpf - WinForm转换为Wpf

    wpf - WPF +温莎城堡+ MVVM : Locator-DataContext

    wpf - MVVM - 重用具有多个 ViewModel 的 View

    c# - EventToCommand 永远不会被触发 - MVVM

    c# - 如何在 XAML 中的 'created' 代码隐藏中访问控制

    c# - 将功能区文本框上的 IsEnabled 设置为 true 什么都不做

    C# DispatcherHelper.CheckBeginInvokeOnUI 不会在 UnitTest 上运行

    windows-phone-8 - Windows Phone 8.1 - MVVMLight - 为什么 EventToCommad 不起作用?

    wpf - .NET Framework 3.0 (WPF) 的 Visual Studio 2005 扩展

    wpf - WPF ComboBox itemsSource在Prism MVVM中不起作用