mister_b
mister_b

Reputation: 307

Binding to margin as a setter value of a style

I'm trying to bind the margin of my chart axis labels to a property.

I thought it would be a simple case of the below code (it works without the binding).

XAML

           <DVC:LinearAxis Orientation="X" Interval="0.5" ShowGridLines="True">
                <DVC:LinearAxis.AxisLabelStyle>
                    <Style TargetType="{x:Type DVC:AxisLabel}">
                        <Setter Property="Margin" Value="{Binding LabelMargin}" />
                    </Style>
                </DVC:LinearAxis.AxisLabelStyle>
            </DVC:LinearAxis>

View Model

private Thickness _labelMargin;

public Thickness LabelMargin
        {
            get { return _labelMargin; }
            set { SetPropertyAndNotify(ref _labelMargin, value); }
        }

This has no effect on the margin, any ideas as to what I'm doing wrong?

EDIT: I'm getting the following error in the output window

System.Windows.Data Error: 40 : BindingExpression path error: 'LabelMargin' property not found on 'object' ''Double' (HashCode=1072693248)'. BindingExpression:Path=LabelMargin; DataItem='Double' (HashCode=1072693248); target element is 'NumericAxisLabel' (Name=''); target property is 'Margin' (type 'Thickness')

Upvotes: 0

Views: 288

Answers (1)

user2250152
user2250152

Reputation: 20660

What about something like this:

<DVC:LinearAxis Orientation="X" Interval="0.5" ShowGridLines="True">
   <DVC:LinearAxis.AxisLabelStyle>
      <Style TargetType="{x:Type DVC:AxisLabel}">
         <Setter Property="Margin" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DVC:LinearAxis}}, Path=DataContext.LabelMargin}" />
      </Style>
   </DVC:LinearAxis.AxisLabelStyle>
</DVC:LinearAxis>

Upvotes: 1

Related Questions