C# 带绑定的标签属性ContentStringFormat不';t刷新
我希望能够在运行时更改只读数据的显示格式。我发现标签具有ContentStringFormat属性。我将ContentStringFormat绑定到DependencyProperty,但当我更改它时,标签的内容不会更改C# 带绑定的标签属性ContentStringFormat不';t刷新,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我希望能够在运行时更改只读数据的显示格式。我发现标签具有ContentStringFormat属性。我将ContentStringFormat绑定到DependencyProperty,但当我更改它时,标签的内容不会更改 <Window x:Class="TestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.m
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="Window">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" x:Name="Label" ContentStringFormat="{Binding ElementName=Window,
Path=StringFormat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<StackPanel Orientation="Horizontal" Grid.Row="2">
<Button Content="Add accuracy" Click="ButtonAdd_OnClick"/>
<Button Content="Reduce accuracy" Click="ButtonReduce_OnClick"/>
</StackPanel>
使用System.ComponentModel;
使用System.Runtime.CompilerServices;
使用System.Windows;
使用System.Windows.Controls;
使用TestApp.annotation;
命名空间TestApp
{
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共静态只读DependencyProperty StringFormatProperty=DependencyProperty.Register(“StringFormat”、typeof(字符串)、typeof(标签),
新属性元数据(空);
公共字符串格式
{
获取{return(string)GetValue(StringFormatProperty);}
set{SetValue(StringFormatProperty,value);}
}
公共主窗口()
{
初始化组件();
标签内容=10m;
StringFormat=“f2”;
OnPropertyChanged(“StringFormat”);
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
私有无效按钮单击(对象发送方,路由目标)
{
var num=int.Parse(StringFormat.Substring(1));
StringFormat=“f”+(num+1);
}
私有void按钮reduce_OnClick(对象发送方、路由目标方)
{
var num=int.Parse(StringFormat.Substring(1));
如果(num)所有者类型应该是MainWindow
而不是Label
,那么它应该是dependencProperty.Register(“StringFormat”、typeof(string)、typeof(MainWindow)…
,否则GetValue(StringFormatProperty)
将返回null
(但任何异常都将隐式接受)和SetValue(StringFormatProperty、value)
将无效。谢谢,但没有任何更改。它实际上更改了ContentStringFormat
OK,但即使在代码中手动更改ContentStringFormat
时,您也会发现它也不起作用。这使得ContentStringFormat的绑定几乎毫无用处。如果您尝试设置一些值(即使是当前值)对于Label.Content
,您将看到它是有效的。我可以确认此问题也发生在我的项目中。ContentStringFormat仅用于标签的初始化。因此,在我的示例中,我将使用Visibility属性切换两个标签。所有者类型应为MainWindow
而不是Label
,因此应为De>pendencyProperty.Register(“StringFormat”、typeof(string)、typeof(MainWindow)…
否则GetValue(StringFormatProperty)
将返回null
(但任何异常都将隐式接受)和SetValue(StringFormatProperty,value)
将无效。谢谢,但没有任何更改。它实际上更改了ContentStringFormat
OK,但即使在代码中手动更改ContentStringFormat
时,您也会发现它也不起作用。这使得ContentStringFormat的绑定几乎毫无用处。如果您尝试设置一些值(即使是当前值)对于Label.Content
,您将看到它是有效的。我可以确认此问题也发生在我的项目中。ContentStringFormat仅在标签初始化时使用。因此在我的示例中,我将使用Visibility属性切换两个标签。
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using TestApp.Annotations;
namespace TestApp
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register("StringFormat", typeof(string), typeof(Label),
new PropertyMetadata(null));
public string StringFormat
{
get { return (string)GetValue(StringFormatProperty); }
set { SetValue(StringFormatProperty, value); }
}
public MainWindow()
{
InitializeComponent();
Label.Content = 10m;
StringFormat = "f2";
OnPropertyChanged("StringFormat");
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private void ButtonAdd_OnClick(object sender, RoutedEventArgs e)
{
var num = int.Parse(StringFormat.Substring(1));
StringFormat = "f" + (num + 1);
}
private void ButtonReduce_OnClick(object sender, RoutedEventArgs e)
{
var num = int.Parse(StringFormat.Substring(1));
if(num<=0)
return;
StringFormat = "f" + (num - 1);
}
}
}