我遇到错误。这是场景:
加载
谁能帮我解决这个问题。如果有帮助(不是那么大),我很乐意上传整个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/