DynamicResource an property eines UserControls
-
heiho,
es geht darum, ich hab strings in einer eigenen xaml datei, diese wollen mittels dynamicresource an einen usercontrol gebunden werden
<Window.Resources> <sys:String x:Key="IDS_AA">aa</sys:String> </Window.Resources> <Grid> <StackPanel> <Button Width="50" Height="20" Content="{DynamicResource IDS_AA}" /> <local:Own Width="50" Height="70" content="{DynamicResource IDS_AA}" /> <!-- Dieses control --> </StackPanel> </Grid>
Das funktioniert aber nicht mit der meldung DynamicResourceExtensions koennen nur an DependencyProperty properties gebunden werden
da dachte ich - hmm - gut - packste n typeconverter zwischen
nur dieser wird anscheinend nicht aufgerufenpublic partial class Own : UserControl { public Own() { InitializeComponent(); } public Bla content { get; set; } } public class BlaConverter : TypeConverter { public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { return (string)value; } } [TypeConverter(typeof(BlaConverter))] public class Bla { public string content { get; set; } }
daher die frage
wie kann ich einen string mittels DynamicResource an einen UserControl property zuweisen?
(bisher kann ich es nur per sourcecode zuweisen)
-
ich hab das nu so geloest das ich die variable als DependencyProperty eingestellt hab - dachte es gibt ne bessere methode
<Window.Resources> <sys:String x:Key="IDS_AA">aa</sys:String> </Window.Resources> <Grid> <StackPanel> <Button Width="50" Height="20" Content="{DynamicResource IDS_AA}" /> <local:Own Width="50" Height="70" content="{DynamicResource IDS_AA}" /> </StackPanel> </Grid>
public partial class Own : UserControl { public Own() { InitializeComponent(); } public static readonly DependencyProperty contentProperty = DependencyProperty.Register("content", typeof(string), typeof(Own), new UIPropertyMetadata(contentValueChanged)); public string content { get { return (string)GetValue(contentProperty ); } set { SetValue(contentProperty , value); } } private static void contentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Own own = (Own)d; // own.XXX = (string)e.NewValue; // use the value } }