我遇到错误。这是场景:

加载

  • wpf应用程序,并将ScrollViewer的内容绑定(bind)到名为ActiveFunction(类型为UserControl)的ViewModel中的属性。在该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/

    10-13 08:24