eocron
eocron

Reputation: 7546

OxyPlot.WPF how to change PlotView styles?

Is there any way to change line color from XAML, not from C#?

<UserControl x:Class="ProceduralWorldGenerator.Views.SplineEditorView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:oxy="http://oxyplot.org/wpf"
             xmlns:splines="clr-namespace:ProceduralWorldGenerator.ViewModels.Splines"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <splines:SplineEditorViewModel/>
    </UserControl.DataContext>
    <oxy:PlotView Model="{Binding Plot}" Background="{DynamicResource NodifyEditor.BackgroundBrush}" Foreground="{DynamicResource NodifyEditor.ForegroundBrush}">
       <?????? {DynamicResource MyLineBrush}>
    </oxy:PlotView>
</UserControl>

I have styles described in ResourceDictionary and It would be nice if my ViewModel just can implement some property like LineBrush and just set it from XAML.

Upvotes: -1

Views: 102

Answers (1)

erray
erray

Reputation: 121

Yes, you can achieve this by defining a DependencyProperty in your UserControl's code-behind (the .cs file) and binding it to your ViewModel's property. Here's how you can do it.

Code-behind:

using System.Windows;
using System.Windows.Media;

namespace ProceduralWorldGenerator.Views
{
    public partial class SplineEditorView : UserControl
    {
        public static readonly DependencyProperty LineBrushProperty =
            DependencyProperty.Register("LineBrush", typeof(Brush), typeof(SplineEditorView), new PropertyMetadata(null));

        public Brush LineBrush
        {
            get { return (Brush)GetValue(LineBrushProperty); }
            set { SetValue(LineBrushProperty, value); }
        }

        public SplineEditorView()
        {
            InitializeComponent();
        }
    }
}

XAML

<UserControl x:Class="ProceduralWorldGenerator.Views.SplineEditorView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:oxy="http://oxyplot.org/wpf"
             xmlns:splines="clr-namespace:ProceduralWorldGenerator.ViewModels.Splines"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="Root">
    <UserControl.DataContext>
        <splines:SplineEditorViewModel/>
    </UserControl.DataContext>
    <oxy:PlotView Model="{Binding Plot}" Background="{DynamicResource NodifyEditor.BackgroundBrush}" Foreground="{DynamicResource NodifyEditor.ForegroundBrush}">
        <oxy:PlotView.LineSeriesColor>
            <Binding ElementName="Root" Path="LineBrush"/>
        </oxy:PlotView.LineSeriesColor>
    </oxy:PlotView>
</UserControl>

Upvotes: 1

Related Questions