这个PlotModel已被其他一些PlotView控件使用

时间:2016-01-06 09:49:23

标签: c# wpf oxyplot

如果考虑这个question

  

PlotModel封装在该PlotView对象中。

我使用的绑定不是相同的PlotModel,而是绑定到不同的PlotViews。但它不起作用。为什么?* *

的Xaml:

<Window.Resources>
    <Style x:Key="FirstStyle" TargetType="{x:Type UniformGrid}">
        <Setter Property="Rows" Value="1"/>
        <Setter Property="Columns" Value="1"/>
    </Style>
    <Style x:Key="SecondStyle" TargetType="{x:Type UniformGrid}">
        <Setter Property="Rows" Value="2"/>
        <Setter Property="Columns" Value="2"/>
    </Style>
    <Style x:Key="Third" TargetType="{x:Type UniformGrid}">
        <Setter Property="Rows" Value="2"/>
        <Setter Property="Columns" Value="4"/>
    </Style>
    <Style x:Key="FirstStyleItem" TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border CornerRadius="5" BorderThickness="2" BorderBrush="Red">
                        <oxy:PlotView Model="{Binding PlotModel}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="SecondStyleItem" TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border BorderThickness="2" BorderBrush="Yellow">
                        <oxy:PlotView Model="{Binding PlotModel}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ThirdStyleItem" TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border BorderThickness="2" BorderBrush="Yellow" CornerRadius="5"></Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ItemsPanelTemplate x:Key="OneBoxeStyle">
        <UniformGrid Columns="1" Rows="1"/>
    </ItemsPanelTemplate>
    <ItemsPanelTemplate x:Key="FourBoxeStyle">
        <UniformGrid Columns="2" Rows="2"/>
    </ItemsPanelTemplate>
    <ItemsPanelTemplate x:Key="EightBoxeStyle">
        <UniformGrid Columns="4" Rows="2"/>
    </ItemsPanelTemplate>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <StackPanel>
        <RadioButton Content="1">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <ei:ChangePropertyAction TargetName="List" PropertyName="ItemsPanel" Value="{StaticResource OneBoxeStyle}"/>
                    <ei:ChangePropertyAction TargetName="List" PropertyName="ItemContainerStyle" Value="{StaticResource FirstStyleItem}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </RadioButton>
        <RadioButton Content="4">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <ei:ChangePropertyAction TargetName="List" PropertyName="ItemsPanel" Value="{StaticResource FourBoxeStyle}"/>
                    <ei:ChangePropertyAction TargetName="List" PropertyName="ItemContainerStyle" Value="{StaticResource SecondStyleItem}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </RadioButton>
        <RadioButton Content="8"/>
    </StackPanel>
    <ListView  Grid.Column="1" x:Name="List" ItemsSource="{Binding Items}" ItemsPanel="{StaticResource OneBoxeStyle}" ItemContainerStyle="{StaticResource FirstStyleItem}"/>
</Grid>

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new Data();
    }
}

public class Item
{
    public OxyPlot.Wpf.PlotView PlotView { get; set; }

    public PlotModel PlotModel
    {
        get
        {
            return this.PlotView.Model;
        }

        set
        {
            this.PlotView.Model = value;
        }
    }
}

public class Data
{
    public ObservableCollection<Item> Items { get; set; }

    public Data()
    {
        Items = new ObservableCollection<Item>();

        OxyPlot.Wpf.PlotView PV = new OxyPlot.Wpf.PlotView();

        PlotModel PM = new PlotModel();
        PM = new PlotModel();
        LineSeries LS = new LineSeries();
        Random rnd = new Random();
        for (int i = 0; i < 10; i++)
        {
            LS.Points.Add(new DataPoint(i, rnd.Next()));
        }
        PM.Series.Add(LS);
        PM.InvalidatePlot(true);
        PV.Model = PM;
        Items.Add(new Item { PlotView = PV });
    }
}

0 个答案:

没有答案