我有一个包含很多输入参数的视图。每个参数由一个标签(参数名称),一个文本框(用于其值)和另一个标签(用于其单位)(例如kNm)组成。
控件显示在水平Stackpanel中。
更改文本框的文本(ToWay绑定)时,基础属性(例如DesignLife)不再更新,因为我从纯WPF(OnPropertyChanged)切换为MVVM Light(RaisePropertyChanged)。
以下是一些相关的代码段:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace xxxx.Views.CustomControls
{
public class InputParameter : ContentControl
{
static InputParameter()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(InputParameter), new FrameworkPropertyMetadata(typeof(InputParameter)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
// some properties left out
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(InputParameter));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
}
}
<UserControl
x:Class="xxxx.Views.MainUserControls.Column"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:xxxx.Views.CustomControls"
xmlns:cf="clr-namespace:xxxx.Model.Config"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tt="clr-namespace:xxxx.Properties"
DataContext="{Binding Main.Calc, Source={StaticResource Locator}}"
mc:Ignorable="d">
<GroupBox Margin="3">
<StackPanel Orientation="Vertical">
<!-- Stuff left out -->
<cc:InputParameter
NameContent="Design life"
Text="{Binding DesignLife, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, StringFormat=N0}"
UnitContent="years" />
<!-- Stuff left out -->
</StackPanel>
</GroupBox>
</UserControl>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:CoCa.Views.CustomControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Style x:Key="InputParm" TargetType="{x:Type cc:InputParameter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type cc:InputParameter}">
<StackPanel Style="{StaticResource SPanel}">
<Label
x:Name="PART_NameLabel"
Width="130"
HorizontalContentAlignment="Left"
Content="{TemplateBinding NameContent}"
Foreground="{TemplateBinding Foreground}"
Style="{StaticResource LabelStyle1}"
ToolTip="{TemplateBinding ToolTip}" />
<TextBox
x:Name="PART_TextBox"
Width="70"
Background="{TemplateBinding TbBackground}"
Foreground="{TemplateBinding Foreground}"
Style="{StaticResource Tb}"
Text="{TemplateBinding Property=Text}" />
<Label
x:Name="PART_UnitLabel"
Content="{TemplateBinding UnitContent}"
Foreground="{Binding ElementName=PART_NameLabel, Path=Foreground}"
Style="{StaticResource Unit}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
private int _designLife;
public int DesignLife
{
get => _designLife;
set
{
Set<int>(() => DesignLife, ref _designLife, value, true);
}
}
我怀疑RaisePropertyChanged不会被调用,因为在类InputParameter的Text DependencyProperty中没有使用它。 (也许在幕后吗?)
问题:
1:我制作InputParameter的方法是否可行?
2:我的怀疑正确吗?
3 a:如果是这样,我如何获得RaisePropertyChanged的电话?
b:如果不是这样,可能是什么问题?