Binding to DependencyObject Properties
Posted: 17 Feb 2021, 17:03
I want to pass some objects (of a particular type) to a DependencyProperty in XAML, and have the values of properties on those objects set via bindings.
I've tried a number of methods, but none have worked.
This was my latest attempt:
C#
XAML:
Could you explain why this doesn't work? And perhaps suggest another method to achieve a similar result?
Thanks!
I've tried a number of methods, but none have worked.
This was my latest attempt:
C#
Code: Select all
public class ParameterDependencyObject : DependencyObject
{
public static readonly DependencyProperty KeyProperty =
DependencyProperty.Register(nameof(Key), typeof(string), typeof(ParameterDependencyObject), new PropertyMetadata(null));
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(string), typeof(ParameterDependencyObject), new PropertyMetadata(null));
public string Key
{
get { return (string)GetValue(KeyProperty); }
set { SetValue(KeyProperty, value); }
}
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
}
[ContentProperty("Parameters")]
public class TextParametersDependencyObject : DependencyObject
{
public ObservableCollection<ParameterDependencyObject> Parameters
{
get { return (ObservableCollection<ParameterDependencyObject>)GetValue(ParametersProperty); }
set { SetValue(ParametersProperty, value); }
}
// Using a DependencyProperty as the backing store for Parameters. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ParametersProperty =
DependencyProperty.Register("Parameters", typeof(ObservableCollection<ParameterDependencyObject>), typeof(TextParametersDependencyObject), new PropertyMetadata(null));
}Code: Select all
<common:TextContentControl HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource common_style_hermes_textShadowedNormal1}" Text="((common_txt_tripleInsertionTEST))" >
<!-- TextContentControl has a TextParametersDependencyObject TextFormatParametersProperty -->
<common:TextContentControl.TextFormatParameters>
<viewModels:TextParametersDependencyObject>
<!-- Works fine -->
<viewModels:ParameterDependencyObject Key="Key1" Value="This works"/>
<!-- Error in Unity "StaticResource 'airshipHUD_string_test' not found." -->
<viewModels:ParameterDependencyObject Key="Key2" Value="{StaticResource airshipHUD_string_test}" />
<!-- Error in Unity "StaticResource 'airshipHUD_string_test' not found." -->
<viewModels:ParameterDependencyObject Key="Key3" Value="{Binding CurrentSpeed}" />
</viewModels:TextParametersDependencyObject>
</common:TextContentControl.TextFormatParameters>
</common:TextContentControl>
Thanks!
