【问题标题】:ContentStringFormat binding doesn't refresh on propertychangeContentStringFormat 绑定不会在 propertychange 上刷新
【发布时间】:2023-03-16 20:51:01
【问题描述】:

我遇到了一个问题,导致标签在其内容未更改但其格式发生更改时刷新。 ContentStringFormat 属性绑定到视图模型并通知属性更改,但标签没有更新,请在下面找到代码中的最小复制示例以及准备编译/运行的项目来演示该问题。

下载项目:https://www.dropbox.com/s/rjs1lot09uc2lgj/WPFFormatBindingRefresh.zip?dl=0

XAML:

<StackPanel>
    <Label Content="{Binding FirstLabelContent}"></Label>
    <Label Content="{Binding SecondLabelContent}" ContentStringFormat="{Binding SecondLabelFormatContent}"></Label>
    <Button Click="Button_Click">Add "test" to all bound elements</Button>        
</StackPanel>

后面的代码:

    public event PropertyChangedEventHandler PropertyChanged = (a,b)=> { }; // empty handler avoids checking for null when raising

    public string FirstLabelContent { get; set; } = "First Label";
    public string SecondLabelContent { get; set; } = "Second";
    public string SecondLabelFormatContent { get; set; } = "{0} Label";

    void PropertyChange(string PropertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        FirstLabelContent += " TEST";
        SecondLabelFormatContent += " TEST";
        PropertyChange("FirstLabelContent"); // First label correctly updates
        PropertyChange("SecondLabelFormatContent"); // Second label doesn't update, expected behavior is changing the format string should cause an update
    }

【问题讨论】:

  • 你检查VS输出看是否有绑定错误?或任何 xaml 警告?
  • @Mishka 没有警告,在更新之前运行得很好,所以绑定也很好,不是拼写错误,因为我在真实项目中的每次出现时都会遇到这个问题,正如你所看到的复制示例可以几乎不简单
  • 也许您需要为 SecondLabelContent 提高 PropertyChanged,否则它不会再次“获取”字符串格式?
  • @Mishka 这是不可能的,因为 secondlabelcontent 没有改变,这就是为什么我想要更新 UI 的原因,我制作了一个简单的复制示例,因为它是显示问题的标准方式但在我的实际项目中,找出必须更改的 100 个属性是不行的,我将数据绑定到格式字符串,我希望它在更改时更新,这是它适用于 wpf 中其他任何东西的方式所以我正在尝试查看我是否偶然发现了 wpf 错误,或者我是否做错了什么以及是否有解决方法。
  • 解决方法:使用MultiBinding 并自己格式化文本

标签: c# .net wpf mvvm data-binding


【解决方案1】:

Label 不支持通过绑定刷新ContentStringFormat

您可以像这样使用多转换器:

public class MultiConverter2 : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string SecondLabelContent = values[0] as string;
        string SecondLabelFormatContent = values[1] as string;

        return string.Format(SecondLabelFormatContent, SecondLabelContent);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

XAML:

<StackPanel>
    <StackPanel.Resources>
        <local:MultiConverter2 x:Key="conv" />
    </StackPanel.Resources>
    <Label Content="{Binding FirstLabelContent}"></Label>
    <Label>
        <Label.Content>
            <MultiBinding Converter="{StaticResource conv}">
                <Binding Path="SecondLabelContent" />
                <Binding Path="SecondLabelFormatContent" />
            </MultiBinding>
        </Label.Content>
    </Label>
    <Button Click="Button_Click">Add "test" to all bound elements</Button>
</StackPanel>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 2011-04-05
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    相关资源
    最近更新 更多