更改wpf静态资源的值

Cad*_*ris 6 c# wpf resourcedictionary

如何在运行时更改WPF静态资源的值?

我有以下资源

<UserControl.Resources>
    <sys:String x:Key="LengthFormat">#.# mm</sys:String>
    <sys:String x:Key="AreaFormat">#.# mm²</sys:String>
    <sys:String x:Key="InertiaFormat">#.# mm?</sys:String>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

一些文字块参考

<TextBlock Grid.Row="2" Grid.Column="1" 
 Text="{Binding Path=Breadth, StringFormat={StaticResource ResourceKey=LengthFormat}}" />
Run Code Online (Sandbox Code Playgroud)

然后根据要绑定到控件的对象,我想改变格式.我在控件中设置了如下属性:

public string LengthFormat
{
    set
    {
        this.Resources["LengthFormat"] = value;
    }
}
public string AreaFormat
{
    set
    {
        this.Resources["AreaFormat"] = value;
    }
}
public string InertiaFormat
{
    set
    {
        this.Resources["InertiaFormat"] = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在绑定之前我设置每个字符串.

但它不起作用,有人建议whynot?

干杯

Cla*_*sen 3

事实上它工作得很好。但 UI 并未更新,因为未观察到资源键。

如果您想要可以更改的绑定,则不应使用静态资源。请改用常规绑定,并INotifyPropertyChanged使用属性,允许 UI 观察更改。