gpt4 book ai didi

mvvm - BindingMode.TwoWay 不适用于 UserControl(不更新源属性)

转载 作者:行者123 更新时间:2023-12-03 10:16:48 25 4
gpt4 key购买 nike

我创建了包含 TextBox 和 PasswordBox 的自定义用户控件。它绑定(bind)完全有效,但是当我更改用户控件的 TextBox 或 PasswordBox 内的任何值时,我的源属性不会被刷新。

以下是我的自定义用户控件的代码

RestrictedBox.xaml

<UserControl.Resources>
<Converters:EnumToVisibilityConverter x:Key="enumToVisibilityConverter" />
<Converters:EnumToVisibilityConverterReverse x:Key="enumToVisibilityConverterReverse" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent" >
<StackPanel>
<TextBox x:Name="txtTextBox" Width="50" Height="25" />
<PasswordBox x:Name="txtPasswordBox" Width="50" Height="25" />
</StackPanel>
</Grid>

RestrictedBox.xaml.cs
public partial class RestrictedBox : UserControl
{
public RestrictedBox()
{
InitializeComponent();
txtTextBox.SetBinding(TextBox.TextProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay });
txtTextBox.SetBinding(TextBox.VisibilityProperty, new Binding("Type")
{
Source = this,
Converter = new EnumToVisibilityConverter()
});
txtPasswordBox.SetBinding(PasswordBox.PasswordProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay });
txtPasswordBox.SetBinding(TextBox.VisibilityProperty, new Binding("Type")
{
Source = this,
Converter = new EnumToVisibilityConverterReverse()
});
}
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(RestrictedBox), new PropertyMetadata("", ValueChanged));
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public Mode Type
{
get { return (Mode)GetValue(TypeProperty); }
set { SetValue(TypeProperty, value); }
}
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(Mode), typeof(RestrictedBox), new PropertyMetadata(Mode.Text, TypeChanged));
private static void TypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
}

以下是我使用自定义用户控件(RestrictedBox)的 LoginView 的代码。

LoginView.xaml
<control:RestrictedBox Type="Text" Value="{Binding Path=UserName}" />

LoginView.xaml.cs
 [Export(typeof(LoginView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class LoginView : UserControl, IPageTitle
{
#region Constuctors
public LoginView()
{
InitializeComponent();
}
[Import]
public LoginViewModel ViewModel
{
get
{
return this.DataContext as LoginViewModel;
}
set
{
DataContext = value;
}
}
#endregion
}

LoginViewModel.cs
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class LoginViewModel : INotifyPropertyChanged, IRegionMemberLifetime
{
private string _UserName = "";
public string UserName
{
get { return _UserName; }
set
{
_UserName = value;
OnPropertyChanged("UserName");
}
}
[ImportingConstructor]
public LoginViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
{
}
}

请帮我解决这个问题,因为我从过去 1.5 天开始一​​直在尝试解决,但没有任何运气。

您的意见和建议将不胜感激。

注意:- 我能够将 UserName 的值绑定(bind)到 TextBox,但我更新了 TextBox 并单击提交,我无法从 TextBox 获取更新的值。

谢谢,

伊姆达杜森

最佳答案

您在 LoginView.xaml 中缺少 Mode=TwoWay:

<control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}" />

关于mvvm - BindingMode.TwoWay 不适用于 UserControl(不更新源属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9483919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com