xaml - 内容控件不更新文本
问题描述
我有一个包含很多输入参数的视图。每个参数由一个标签(参数名称)、一个用于其值的文本框和另一个用于其单位的标签(例如 kNm)组成。
控件显示在水平堆栈面板中。
当更改文本框的文本(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:如果不是:可能是什么问题?
解决方案
推荐阅读
- wordpress - 我可以通过 $query_string 和另一个 args 组合的 args 进行 FOREACH 吗?
- vue.js - 如何在 vuex 中过滤多个获取数据的键?
- node.js - Nodejs 14.5 文件系统 API Dirent“符号(类型)”属性是什么?
- vespa - vespa 安装有什么变化吗?
- ios - 在swiftUI中捕获onLongPressGesture的touchDown位置?
- go - golang 中的 defer mutex.Unlock() 有时从未调用过
- azure-eventgrid - 事件网格 webhook 握手验证失败
- mysql - MySQL:左连接、连接或求和、未重复的行
- c - 在 C 循环中重复在堆栈上创建缓冲区是不好的做法吗?
- struct - rust - 如何使用派生宏来解析结构的元信息?