C# 无法以编程方式从MainWindowViewModel中读取更新的用户控件属性
在我的WPF应用程序中有以下代码。我正在使用ContentPresenter控件在更改SelectedAccountType组合框值时动态呈现主窗口中的用户控件。 我在UI的MyProperty文本框中输入了一些值,然后单击MainWindow上的Save按钮。但是我在Save方法的MainWindowViewModel.cs中没有看到这个值(即单击Save按钮)。 所有my ViewModels都扩展了此抽象类:C# 无法以编程方式从MainWindowViewModel中读取更新的用户控件属性,c#,wpf,C#,Wpf,在我的WPF应用程序中有以下代码。我正在使用ContentPresenter控件在更改SelectedAccountType组合框值时动态呈现主窗口中的用户控件。 我在UI的MyProperty文本框中输入了一些值,然后单击MainWindow上的Save按钮。但是我在Save方法的MainWindowViewModel.cs中没有看到这个值(即单击Save按钮)。 所有my ViewModels都扩展了此抽象类: public abstract class ViewModelBase : IN
public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
请问我这里缺什么
谢谢
这是我的密码:
MainWindow.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MainViewModel="clr-namespace:Test.ViewModel"
xmlns:ViewModel="clr-namespace:Test.ViewModel.AccountTypes"
xmlns:View="clr-namespace:Test.View" x:Class="Test.MainWindow"
xmlns:Views="clr-namespace:Test.View.AccountTypes"
xmlns:v="clr-namespace:Test.View.AccountTypes"
xmlns:vm="clr-namespace:Test.ViewModel.AccountTypes"
Title="{Binding DisplayName, Mode=OneWay}" ResizeMode="CanResize" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<MainViewModel:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<StackPanel Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Height="28" Width="auto" Margin="5,0,0,0">
<ComboBox Width="360" Margin="1,0" ItemsSource="{Binding AccountTypes}" DisplayMemberPath="Code" SelectedValuePath="ID" SelectedItem="{Binding SelectedAccountType,
Mode=TwoWay}" TabIndex="0" />
</StackPanel>
<ContentPresenter Content="{Binding CurrentViewModel}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type ViewModel:AC1ViewModel}">
<Views:AC1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModel:AC2ViewModel}">
<Views:AC2View/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
AC1View.xaml:
<TextBox HorizontalAlignment="Left" Height="23" Margin="1,1,0,0" VerticalAlignment="Top" Width="230" TabIndex="1" Text="{Binding MyProperty,UpdateSourceTrigger=PropertyChanged}" />
“SaveButtonCommand方法”,我想你的意思是
Save()
?MyProperty
是否在该循环中枚举?是的,这是正确的。此外,是的,MyProperty正在该循环中枚举绑定是否正常工作?如果您调试它们,是否可以检查您的属性是否正在设置?在此期间,SelectedAccountType
是否可能更改?这将在CurrentViewModel中生成一个新对象。这很容易用setter中的断点进行检查。但是首先,根据@Forlani,我在AC1ViewModel中的MyProperty
setter中设置了一个断点是的,当我处于调试模式时,我可以看到MyProperty正在使用新值进行设置。只是我在MainViewModel的Save方法中读取更新后的值时没有看到它。谢谢。通过“SaveButtonCommand方法”,我想你的意思是Save()
?MyProperty
是否在该循环中枚举?是的,这是正确的。此外,是的,MyProperty正在该循环中枚举绑定是否正常工作?如果您调试它们,是否可以检查您的属性是否正在设置?在此期间,SelectedAccountType
是否可能更改?这将在CurrentViewModel中生成一个新对象。这很容易用setter中的断点进行检查。但是首先,根据@Forlani,我在AC1ViewModel中的MyProperty
setter中设置了一个断点是的,当我处于调试模式时,我可以看到MyProperty被设置为新值。只是在MainViewModel的Save方法中读取更新后的值时,我看不到它。谢谢。
<TextBox HorizontalAlignment="Left" Height="23" Margin="1,1,0,0" VerticalAlignment="Top" Width="230" TabIndex="1" Text="{Binding MyProperty,UpdateSourceTrigger=PropertyChanged}" />
public class AC1ViewModel
{
private string m_myProperty = "";
public AC1ViewModel()
{
}
public string MyProperty
{
get
{
return m_myProperty;
}
set
{
m_myProperty = value;
}
}
}