c# - ContentStringFormat 绑定(bind)不会在 propertychange 上刷新

标签 c# .net wpf mvvm data-binding

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

下载项目: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
    }

最佳答案

Label不支持刷新ContentStringFormat通过绑定(bind)。

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

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>

关于c# - ContentStringFormat 绑定(bind)不会在 propertychange 上刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45526615/

相关文章:

c# - ASP.NET 中按字母、数字和字符的高级排序实现

.net - Entity Framework .Include()导航另一个导航属性

c# - 删除 Application Insights 后应用程序将无法启动

c# - WPF Mouse.Capture 导致窗口卡住

c# - 我可以对以任何顺序唯一的多个属性设置主键/唯一约束吗?

c# - 我应该如何使用 xamarin 表单在 android 上创建主要 Activity 以支持画中画模式?

c# - WCF Web 服务调用超时

javascript - 如何使用 JavaScript 设置此 ASPX 控件的文本?

WPF DataGrid - 我可以用属性装饰我的 POCO 以具有自定义列名吗?

WPF 动态绑定(bind)到属性