Come posso modificare il valore di una risorsa statica WPF in fase di runtime?Modificare il valore di una risorsa statica wpf
Ho le seguenti risorse
<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>
che alcuni TextBlocks riferimento
<TextBlock Grid.Row="2" Grid.Column="1"
Text="{Binding Path=Breadth, StringFormat={StaticResource ResourceKey=LengthFormat}}" />
poi a seconda dell'oggetto di essere vincolato al controllo vorrei modificare i formati. Ho installato proprietà del controllo come segue:
public string LengthFormat
{
set
{
this.Resources["LengthFormat"] = value;
}
}
public string AreaFormat
{
set
{
this.Resources["AreaFormat"] = value;
}
}
public string InertiaFormat
{
set
{
this.Resources["InertiaFormat"] = value;
}
}
quindi prima di associare i set ogni stringa.
Tuttavia, non funziona, qualcuno suggerisce whynot?
Cheers
Viene visualizzato un errore che dice: Errore 4 Non è possibile impostare "DynamicResourceExtension" nella proprietà "StringFormat" di tipo "Binding". Un 'DynamicResourceExtension' può essere impostato solo su DependencyProperty di DependencyObject. –
ohh Giusto, ha senso. In questo caso non sarai in grado di applicare la mia soluzione. Funzionerà solo per DependencyProperty –