Bind To DP On Control
-
I have created a RadioButton who's style will look similar to [this](http://3.bp.blogspot.com/-hw\_WTwyp4Fo/Tol\_TIu3jXI/AAAAAAAAABU/JjUpdLH1ecQ/s320/radio5.png). In my version the line can be top, left, right, or bottom. In the code behind I did:
public class MaroisRadioButton : RadioButton
{
public enum Position
{
Bottom,
Left,
Right,
Top
}#region DP RadioMarkPosition public static readonly DependencyProperty RadioMarkPositionProperty = DependencyProperty.Register("RadioMarkPosition", typeof(Position), typeof(MaroisRadioButton), new PropertyMetadata(Position.Left)); public Position RadioMarkPosition { get { return (Position)GetValue(RadioMarkPositionProperty); } set { SetValue(RadioMarkPositionProperty, value); } } #endregion #region DP RadioMarkSize public static readonly DependencyProperty RadioMarkSizeProperty = DependencyProperty.Register("RadioMarkSize", typeof(double), typeof(MaroisRadioButton), new PropertyMetadata(null)); public double RadioMarkSize { get { return (double)GetValue(RadioMarkSizeProperty); } set { SetValue(RadioMarkSizeProperty, value); } } #endregion
}
Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:
But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'" How do I bind the Width property to the DP in the code behind? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have created a RadioButton who's style will look similar to [this](http://3.bp.blogspot.com/-hw\_WTwyp4Fo/Tol\_TIu3jXI/AAAAAAAAABU/JjUpdLH1ecQ/s320/radio5.png). In my version the line can be top, left, right, or bottom. In the code behind I did:
public class MaroisRadioButton : RadioButton
{
public enum Position
{
Bottom,
Left,
Right,
Top
}#region DP RadioMarkPosition public static readonly DependencyProperty RadioMarkPositionProperty = DependencyProperty.Register("RadioMarkPosition", typeof(Position), typeof(MaroisRadioButton), new PropertyMetadata(Position.Left)); public Position RadioMarkPosition { get { return (Position)GetValue(RadioMarkPositionProperty); } set { SetValue(RadioMarkPositionProperty, value); } } #endregion #region DP RadioMarkSize public static readonly DependencyProperty RadioMarkSizeProperty = DependencyProperty.Register("RadioMarkSize", typeof(double), typeof(MaroisRadioButton), new PropertyMetadata(null)); public double RadioMarkSize { get { return (double)GetValue(RadioMarkSizeProperty); } set { SetValue(RadioMarkSizeProperty, value); } } #endregion
}
Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:
But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'" How do I bind the Width property to the DP in the code behind? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
The DependencyProperty is on a derived RadioButton and you're attempting to read it on a Border bound to the VM DataContext. That's why your code can't see it. Why not put your derived RadioButton on there and get access to the DPs from that?
-
The DependencyProperty is on a derived RadioButton and you're attempting to read it on a Border bound to the VM DataContext. That's why your code can't see it. Why not put your derived RadioButton on there and get access to the DPs from that?
Here's the full style. The Border is part of the RadioButton Control Template's BulletDecarator:
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type mbc:MaroisRadioButton}"> <BulletDecorator Cursor="Hand"> <Border BorderBrush="LightGray" BorderThickness="2" Margin="2"> <Grid> <Border Grid.Row="1" Grid.Column="0" Margin="2" Background="{DynamicResource buttonPressedBackBrush}" Width="{Binding RadioMarkSize}" HorizontalAlignment="Left" VerticalAlignment="Stretch" Name="LeftRadioMark"/> <ContentPresenter Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2"/> </Grid> </Border> </BulletDecorator> </ControlTemplate> </Setter.Value> </Setter>
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Here's the full style. The Border is part of the RadioButton Control Template's BulletDecarator:
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type mbc:MaroisRadioButton}"> <BulletDecorator Cursor="Hand"> <Border BorderBrush="LightGray" BorderThickness="2" Margin="2"> <Grid> <Border Grid.Row="1" Grid.Column="0" Margin="2" Background="{DynamicResource buttonPressedBackBrush}" Width="{Binding RadioMarkSize}" HorizontalAlignment="Left" VerticalAlignment="Stretch" Name="LeftRadioMark"/> <ContentPresenter Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2"/> </Grid> </Border> </BulletDecorator> </ControlTemplate> </Setter.Value> </Setter>
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Try using
TemplateBinding
instead.<Border
Grid.Row="1"
Grid.Column="0"
Margin="2"
Background="{DynamicResource buttonPressedBackBrush}"
Width="{TemplateBinding RadioMarkSize}"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
Name="LeftRadioMark"
/>TemplateBinding Markup Extension | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Try using
TemplateBinding
instead.<Border
Grid.Row="1"
Grid.Column="0"
Margin="2"
Background="{DynamicResource buttonPressedBackBrush}"
Width="{TemplateBinding RadioMarkSize}"
HorizontalAlignment="Left"
VerticalAlignment="Stretch"
Name="LeftRadioMark"
/>TemplateBinding Markup Extension | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
That did it. Thanks! ya learnt me something
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have created a RadioButton who's style will look similar to [this](http://3.bp.blogspot.com/-hw\_WTwyp4Fo/Tol\_TIu3jXI/AAAAAAAAABU/JjUpdLH1ecQ/s320/radio5.png). In my version the line can be top, left, right, or bottom. In the code behind I did:
public class MaroisRadioButton : RadioButton
{
public enum Position
{
Bottom,
Left,
Right,
Top
}#region DP RadioMarkPosition public static readonly DependencyProperty RadioMarkPositionProperty = DependencyProperty.Register("RadioMarkPosition", typeof(Position), typeof(MaroisRadioButton), new PropertyMetadata(Position.Left)); public Position RadioMarkPosition { get { return (Position)GetValue(RadioMarkPositionProperty); } set { SetValue(RadioMarkPositionProperty, value); } } #endregion #region DP RadioMarkSize public static readonly DependencyProperty RadioMarkSizeProperty = DependencyProperty.Register("RadioMarkSize", typeof(double), typeof(MaroisRadioButton), new PropertyMetadata(null)); public double RadioMarkSize { get { return (double)GetValue(RadioMarkSizeProperty); } set { SetValue(RadioMarkSizeProperty, value); } } #endregion
}
Then I created style for it and in the style I want to bind the RadioMarkSize to the Width property in the XAML:
But I can't get the binding to work. I get a binding error "BindingExpression path error: 'RadioMarkSize' property not found on 'object' ''MainWindowViewModel'" How do I bind the Width property to the DP in the code behind? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Did you set the DataContext to your custom create class called "MaroisRadioButton":confused: I believe in the code-behind you need to set the DataContext to this custom class so XAML engine can locate this custom dependency property.
Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach