寻找一种方法来更新 MVVM 应用程序中的非依赖项 属性
Looking for a way to update a non-dependency property in MVVM application
我正在用 WPF 编写终端应用程序。我从嵌入式设备接收字符,并更新绑定到我的 ViewModel 的 TextBox.Text 属性。
问题是当我更新文本 属性 时,文本框插入符被重置。我想做的是在我的 viewModel 中保留一个 Caret 参数并将其绑定到 TextBox 的 Caret 属性,但是 TextBox Caret 不是依赖项 属性,我不想访问直接来自我的视图模型的视图。
您是否熟悉不破坏 MVVM 模式的正确解决方案?
提前致谢。
您可以添加附加 属性 来绑定非依赖性 属性。在下面的示例中,我为文本框的 CaretIndex 属性 创建了它。
<Window x:Class="Converter_Learning.Window7"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Converter_Learning"
Title="Window7" Height="500" Width="500">
<Grid FocusManager.FocusedElement="{Binding ElementName=txt}">
<TextBox x:Name="txt" Text="Hiiiiiiiiiiiiiii" local:TextBoxHelper.Caret="{Binding Caret}" />
</Grid>
public partial class Window7 : Window
{
public Window7()
{
InitializeComponent();
this.DataContext= new CaretViewModel();
}
}
public class CaretViewModel : INotifyPropertyChanged
{
private int myVar;
public int Caret
{
get { return myVar; }
set { myVar = value; Notify("Caret"); }
}
public CaretViewModel()
{
Caret = 5;
}
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string property)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public static class TextBoxHelper
{
public static int GetCaret(DependencyObject obj)
{
return (int)obj.GetValue(CaretProperty);
}
public static void SetCaret(DependencyObject obj, int value)
{
obj.SetValue(CaretProperty, value);
}
public static readonly DependencyProperty CaretProperty =
DependencyProperty.RegisterAttached(
"Caret",
typeof(int),
typeof(TextBoxHelper),
new FrameworkPropertyMetadata(0, CaretChanged));
private static void CaretChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TextBox tb = obj as TextBox;
if (tb != null)
{
int newValue = (int)e.NewValue;
if (newValue != tb.CaretIndex)
{
tb.CaretIndex = (int)newValue;
}
}
}
}
我正在用 WPF 编写终端应用程序。我从嵌入式设备接收字符,并更新绑定到我的 ViewModel 的 TextBox.Text 属性。
问题是当我更新文本 属性 时,文本框插入符被重置。我想做的是在我的 viewModel 中保留一个 Caret 参数并将其绑定到 TextBox 的 Caret 属性,但是 TextBox Caret 不是依赖项 属性,我不想访问直接来自我的视图模型的视图。
您是否熟悉不破坏 MVVM 模式的正确解决方案?
提前致谢。
您可以添加附加 属性 来绑定非依赖性 属性。在下面的示例中,我为文本框的 CaretIndex 属性 创建了它。
<Window x:Class="Converter_Learning.Window7"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Converter_Learning"
Title="Window7" Height="500" Width="500">
<Grid FocusManager.FocusedElement="{Binding ElementName=txt}">
<TextBox x:Name="txt" Text="Hiiiiiiiiiiiiiii" local:TextBoxHelper.Caret="{Binding Caret}" />
</Grid>
public partial class Window7 : Window
{
public Window7()
{
InitializeComponent();
this.DataContext= new CaretViewModel();
}
}
public class CaretViewModel : INotifyPropertyChanged
{
private int myVar;
public int Caret
{
get { return myVar; }
set { myVar = value; Notify("Caret"); }
}
public CaretViewModel()
{
Caret = 5;
}
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string property)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
public static class TextBoxHelper
{
public static int GetCaret(DependencyObject obj)
{
return (int)obj.GetValue(CaretProperty);
}
public static void SetCaret(DependencyObject obj, int value)
{
obj.SetValue(CaretProperty, value);
}
public static readonly DependencyProperty CaretProperty =
DependencyProperty.RegisterAttached(
"Caret",
typeof(int),
typeof(TextBoxHelper),
new FrameworkPropertyMetadata(0, CaretChanged));
private static void CaretChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TextBox tb = obj as TextBox;
if (tb != null)
{
int newValue = (int)e.NewValue;
if (newValue != tb.CaretIndex)
{
tb.CaretIndex = (int)newValue;
}
}
}
}