簡體   English   中英

如何將嵌套的視圖模型綁定到控件的屬性

[英]How can I bind the nested viewmodels to properties of a control

我使用Microsoft的WPF工具包圖表控件來編寫自己的圖表控件。 我在這里寫博客。 我的圖表控件將圖表中的yaxs堆疊在一起。 你可以在文章中看到這一切都很有效。 現在我想創建一個控制圖表中數據和軸的視圖模型。 到目前為止,我可以將軸添加到圖表中並在圖表中顯示它們。 但是當我嘗試添加lineseries時我遇到了問題,因為它有一個DependentAxis和一個InDependentAxis屬性。 我不知道如何為它分配正確的xAxis和yAxis控件。
下面你看到LineSeriesViewModel的一部分。 它具有嵌套的XAxisViewModel和YAxisViewModel屬性。

public class LineSeriesViewModel : ViewModelBase, IChartComponent
{

    XAxisViewModel _xAxis;
    public XAxisViewModel XAxis
    {
        get { return _xAxis; }
        set
        {
            _xAxis = value;
            RaisePropertyChanged(() => XAxis);
        }
    }

    //The YAxis Property look the same
}

視圖模型都有自己的datatemplate。 xaml代碼如下所示:

<UserControl.Resources>
    <DataTemplate x:Key="xAxisTemplate" DataType="{x:Type l:YAxisViewModel}">
        <chart:LinearAxis  x:Name="yAxis"  Orientation="Y" Location="Left" Minimum="0"  Maximum="10" IsHitTestVisible="False" Width="50" />
    </DataTemplate>
    <DataTemplate x:Key="yAxisTemplate" DataType="{x:Type l:XAxisViewModel}">
        <chart:LinearAxis x:Name="xAxis"  Orientation="X" Location="Bottom" Minimum="0"  Maximum="100" IsHitTestVisible="False" Height="50" />
    </DataTemplate>

    <DataTemplate DataType="{x:Type l:LineSeriesViewModel}">
        <!--Binding doesn't work on the Dependent and IndependentAxis! -->
        <!--YAxis XAxis and Series are properties of the LineSeriesViewModel -->
        <l:FastLineSeries DependentAxis="{Binding Path=YAxis}" 
                          IndependentAxis="{Binding Path=XAxis}"
                          ItemsSource="{Binding Path=Series}"/>
    </DataTemplate>
    <Style TargetType="ItemsControl">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <!--My stacked chart control -->
                    <l:StackedPanel x:Name="stackedPanel" Width="Auto" Height="Auto" Background="LightBlue">
                    </l:StackedPanel>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True">
    <!-- View is an ObservableCollection of all axes and series-->
    <ItemsControl x:Name="chartItems" ItemsSource="{Binding Path=View}" Focusable="False">
    </ItemsControl>
</Grid>

這段代碼效果很好。 當我添加軸時,它們會被繪制出來。 但是lineseries控件的DependentAxis和InDependentAxis保持為空,因此系列不會被繪制。 如何將嵌套的視圖模型綁定到控件的屬性?

它應該工作。 你可以檢查幾件事:

  • 系列裝訂是否有效? 如果是這樣,試着弄清楚有什么區別。
  • 您確定XAxis和YAxis屬性實際上具有值嗎? 嘗試在getter中放置一個斷點。 如果已達到,則綁定有效。 您還可以在Binding上放置一個轉換器(IValueConverter)(它只返回它接收的值)並在那里放置一個斷點。
  • 在綁定上使用PresentationTraceSources.TraceLevel = High可以獲得更詳細的跟蹤(將出現在VS Output窗口中)。
  • DependentAxis / IndependentAxis是否定義為FastLineSeries上的依賴項屬性?

希望有所幫助,

Aelij。

您可能已經檢查了這一點,但我發現當我調試綁定時,第一個也是最容易啟動的地方是從VS運行調試會話,因為調試輸出告訴哪些對象和屬性無法綁定。 我通常最終發現我需要顯式設置DataContext或其他類似拼寫錯誤。 要查找的輸出如下:

System.Windows.Data錯誤:39:BindingExpression路徑錯誤:

接下來是您嘗試綁定的屬性名稱,通常最重要的是它實際嘗試綁定的類。 如果這沒有幫助,那么這里有關於調試綁定的好文章: http//www.beacosta.com/blog/? p = 52,它討論了Aelij提到的PresentationTraceSources.TraceLevel = High的使用,以及很少有其他技巧。 希望這能走上正軌。

問候,

麥克風

暫無
暫無

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

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