簡體   English   中英

具有依賴項屬性的WPF綁定。 數據不顯示的問題

[英]WPF Binding with Dependency Properties. Issue with Data Not Displaying

我整天都在嘗試讓它運氣不佳。 我正在使用DynamicDataDisplay(D3)顯示圖形。 這是我使用xaml定義的簡單視圖。

<Window x:Class="BMSVM_Simulator.View.GraphWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModel="clr-namespace:BMSVM_Simulator.ViewModel"
        xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
        x:Name="ThisGraphWindowInstance"
        Title="Plot" Height="500" Width="750"
        WindowStartupLocation="CenterOwner"
        Icon="../res/qualcomm_q_icon.ico.ico"
        MinWidth="400" MinHeight="300">

    <Window.DataContext>
        <ViewModel:GraphWindowPresenter/>
    </Window.DataContext>

    <Grid>
        <d3:ChartPlotter Name="plotter" Margin="10,10,20,10">
            <d3:InjectedPlotter Name="innerPlotter" Background="Aqua" SetViewportBinding="False">
                <d3:VerticalAxis Placement="Right"/>
                <d3:VerticalAxisTitle Content="{Binding ElementName=ThisGraphWindowInstance, Path=yAxis2}" Placement="Right"/>
            </d3:InjectedPlotter>      

            <d3:Header FontFamily="Arial" Content="{Binding ElementName=ThisGraphWindowInstance, Path=title}"/>
            <d3:VerticalAxisTitle FontFamily="Arial" Content="{Binding ElementName=ThisGraphWindowInstance, Path=yAxis2}"/>
            <d3:HorizontalAxisTitle FontFamily="Arial" Content="{Binding ElementName=ThisGraphWindowInstance, Path=title}"/>
        </d3:ChartPlotter>
    </Grid>
</Window>

問題是:

<d3:VerticalAxisTitle Content="{Binding ElementName=ThisGraphWindowInstance, Path=yAxis2}" Placement="Right"/>

當我使用Content綁定到Path=yAxis2的當前設置時, InjectedPlotter中的“顯示”根本不顯示。 我在斷點處設置,然后看到yAxis2實際上是一個已定義的字符串,並且它不是null。

當我實際對值進行硬編碼,例如Content="DEFAULT TITLE" ,它就變成:

<d3:VerticalAxisTitle Content="DEFAULT TITLE" Placement="Right"/>

標題顯示正常。

有人知道為什么會這樣嗎?

以下是供參考的代碼:

public static readonly DependencyProperty yAxis2Property =
    DependencyProperty.Register("yAxis2", typeof(string), typeof(GraphWindowView));

public string yAxis2
{
    get { return (string)GetValue(yAxis2Property); }
    set { SetValue(yAxis2Property, value); }
}

public void ShowGraph()
{
    // consume ChartData
    this.yAxis1 = ChartData.yAxisTitle1;
    this.yAxis2 = "AXIS 2 TITLE..SHOW UP!";
     .....
}

編輯>>>>>>>>>

using BMSVM_Simulator.ViewModel;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay.Navigation;
using Microsoft.Research.DynamicDataDisplay.PointMarkers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BMSVM_Simulator.View
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class GraphWindowView : Window
    {
        #region Fields

        private readonly int DEFAULT_AXIS_WIDTH = 20;

        private readonly Pen[] colors = {   
                                            new Pen(Brushes.Blue, 2),
                                            new Pen(Brushes.DarkGreen, 2),
                                            new Pen(Brushes.DarkMagenta, 2),
                                            new Pen(Brushes.DarkSalmon, 2),
                                            new Pen(Brushes.Maroon, 2),
                                            new Pen(Brushes.Orange, 2),
                                            new Pen(Brushes.SkyBlue, 2)
                                        };

        #endregion

        #region DependencyProperties

        public static readonly DependencyProperty yAxis1Property =
            DependencyProperty.Register("yAxis1", typeof(string), typeof(GraphWindowView));

        public static readonly DependencyProperty yAxis2Property =
            DependencyProperty.Register("yAxis2", typeof(string), typeof(GraphWindowView));

        public static readonly DependencyProperty titleProperty =
            DependencyProperty.Register("title", typeof(string), typeof(GraphWindowView));

        public static readonly DependencyProperty xAxisProperty =
            DependencyProperty.Register("xAxis", typeof(string), typeof(GraphWindowView));

        public static readonly DependencyProperty DatesProperty =
            DependencyProperty.Register("Dates", typeof(EnumerableDataSource<int>), typeof(GraphWindowView));

        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(EnumerableDataSource<int>), typeof(GraphWindowView));

        public static readonly DependencyProperty ChartDataProperty =
            DependencyProperty.Register("ChartData", typeof(ChartData), typeof(GraphWindowView));

        public static readonly DependencyProperty rightAxisWidthProperty =
            DependencyProperty.Register("rightAxisWidth", typeof(int), typeof(GraphWindowView));

        public int rightAxisWidth
        {
            get { return (int)GetValue(rightAxisWidthProperty); }
            set { SetValue(rightAxisWidthProperty, value); }
        }

        public ChartData ChartData
        {
            get { return (ChartData)GetValue(ChartDataProperty); }
            set { SetValue(ChartDataProperty, value); }
        }

        public EnumerableDataSource<int> Dates
        {
            get { return (EnumerableDataSource<int>)GetValue(DatesProperty); }
            set { SetValue(DatesProperty, value); }
        }

        public EnumerableDataSource<int> Data
        {
            get { return (EnumerableDataSource<int>)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        public string xAxis
        {
            get { return (string)GetValue(xAxisProperty); }
            set { SetValue(xAxisProperty, value); }
        }

        public string yAxis1
        {
            get { return (string)GetValue(yAxis1Property); }
            set { SetValue(yAxis1Property, value); }
        }

        public string title
        {
            get { return (string)GetValue(titleProperty); }
            set { SetValue(titleProperty, value); }
        }

        public string yAxis2
        {
            get { return (string)GetValue(yAxis2Property); }
            set { SetValue(yAxis2Property, value); }
        }

        #endregion

        public GraphWindowView()
        {
            InitializeComponent();
            rightAxisWidth = DEFAULT_AXIS_WIDTH;
        }

        public void ShowGraph()
        {
            // consume ChartData
            this.xAxis = ChartData.xAxisTitle;
            this.yAxis1 = ChartData.yAxisTitle1;
            this.yAxis2 = "AXIS 2 TITLE..SHOW UP!"; // ChartData.yAxisTitle2;
            this.title = ChartData.title;
            this.rightAxisWidth = DEFAULT_AXIS_WIDTH;

            // list of data points
            List<DataSet> dataSets = this.ChartData.dataPoints;

            int colorCounter = 0;
            int rightAxisCount = 0;
            foreach (DataSet set in dataSets)
            {

                set.dates.SetXMapping(x => x);
                set.data.SetYMapping(x => x);

                CompositeDataSource compositeDataSource1 = new
                    CompositeDataSource(set.dates, set.data);

                if (set.axis == AxisSide.LEFT)
                {
                    plotter.AddLineGraph(compositeDataSource1, colors[colorCounter % colors.Length],
                    new CirclePointMarker { Size = 8.00, Fill = Brushes.Red },
                    new PenDescription(set.legendTitle));
                }
                else
                {
                    innerPlotter.AddLineGraph(compositeDataSource1, colors[colorCounter % colors.Length],
                    new CirclePointMarker { Size = 8.00, Fill = Brushes.Red },
                    new PenDescription(set.legendTitle));

                    rightAxisCount++;
                }


                colorCounter++;
            }

            // if there is nothing plotted against the right axis, don't show it
            if (rightAxisCount == 0)
            {
                rightAxisWidth = 0;
            }

            plotter.Viewport.FitToView();

            // there are duplicate legends, so we hide one
            plotter.LegendVisibility = Visibility.Hidden;

            Show();
        }
    }
}

在提供的代碼中,已將datacontext設置為GraphWindowPresenter的對象,但是在聲明依賴關系屬性時,已設置了GraphWindowView對象。 請確保您將適當的對象設置為datacontext

    <Window.DataContext>
       < ViewModel:GraphWindowPresenter/>
  < /Window.DataContext>

DependencyProperty.Register(“ yAxis2”,typeof(string), typeof(GraphWindowView))

嘗試:

      <d3:VerticalAxisTitle Content="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=yAxis2,Mode=OneWay}" />

我運行了一個綁定TextBox.Text的快速測試,並且您發布的代碼有效。

<Window x:Class="WpfApplication2.MainWindow"
        x:Name="TestWindow" ...>
    <StackPanel>
        <!-- both bindings work -->
        <TextBlock Text="{Binding ElementName=TestWindow, Path=yAxis2}" />
        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=yAxis2}" />
    </StackPanel>
</Window>
public partial class MainWindow : Window
{
    public static readonly DependencyProperty yAxis2Property =
        DependencyProperty.Register("yAxis2", typeof(string), typeof(MainWindow));

    public string yAxis2
    {
        get { return (string)GetValue(yAxis2Property); }
        set { SetValue(yAxis2Property, value); }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.yAxis2 = "TESTING";
    }

}

所以我最好的猜測是

  • 您沒有在窗口上調用ShowGraph()
  • VerticalAxisTitle對象不是可視樹中存在的對象,就像其他一些WPF對象(如DataGridColumn一樣

要確定第一個問題是否是您的問題,只需確保您在窗口后面的構造函數中調用ShowGraph() ,或按照我在此處進行測試的方式設置yAxis2

您還可以使用諸如Snoop之類的工具,它對於調試運行時數據綁定非常有用。

如果完成了該操作,但仍無法正確顯示,則可能需要對VerticalAxisTitle進行更多研究,以找到有關如何正確綁定它的變通辦法。 如果您找不到與VerticalAxisTitle任何內容,請嘗試查找DataGridColumn的完成方式,例如此答案

(作為附帶說明,將公共財產資本化是一種標准慣例,因此您的財產應為YAxis2 。我的OCD才可以使用。):)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM