【问题标题】:Databinding not working on Avalondock window数据绑定在 Avalondock 窗口上不起作用
【发布时间】:2015-06-13 11:04:35
【问题描述】:

我有一个使用 AvalonDock 的带有窗口管理器的项目。

基本上有两个元素:LayoutAnchorableItem 显示我的不同工具箱(目前是一个,由 Treeview 组成)和 LayoutItem 显示使用 treeview 打开的文档(自定义控件,具有可绑定参数 -理论上)

DockingManager 的 ViewModel 托管名为 PanesObservableCollection,这将是 LayoutItems。

如果我不尝试在 XAML 中绑定参数并强制使用这样的值,一切都会“正常”

<avalonDock:DockingManager.LayoutItemTemplateSelector>
    <panes:PanesTemplateSelector>
        <panes:PanesTemplateSelector.ExchangeViewTemplate>
            <DataTemplate>
                <xchng:Exchange/>
            </DataTemplate>
        </panes:PanesTemplateSelector.ExchangeViewTemplate>
        <panes:PanesTemplateSelector.GraphViewTemplate>
            <DataTemplate>
                <grph:Graph TickerCode="ILD" ExchangeCode="EPA"/>
            </DataTemplate>
        </panes:PanesTemplateSelector.GraphViewTemplate>
    </panes:PanesTemplateSelector>
</avalonDock:DockingManager.LayoutItemTemplateSelector>

Exchange 是工具箱,Graph 是 LayoutItems。

对接管理器的初始数据绑定是这样完成的:

<avalonDock:DockingManager Margin="0,0,0,0" 
                         Grid.Row="1"
                         AnchorablesSource="{Binding Tools}"
                         DocumentsSource="{Binding Panes}" 
                         ActiveContent="{Binding ActiveDocument, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}"
                         x:Name="dockManager">

请注意,Pane 的类型为 GraphViewModel,它有两个公共参数:ExchangeCodeTickerCode

问题是我想将 TickerCodeExchangeCode 绑定到 Panes.TickerCode 和 Panes.ExchangeCode 值。

所以我尝试了这个:

<grph:Graph TickerCode="{Binding TickerCode, UpdateSourceTrigger=PropertyChanged}" ExchangeCode="{Binding ExchangeCode, UpdateSourceTrigger=PropertyChanged}"/>

但它什么也不做:自定义控件中的 TickerCode 和 ExchangeCode 等于 "",这与我强制 XAML 中的值相反。

另外有点奇怪的是,如果我介入代码执行,Panes 实际上有 TickerCode 和 ExchangeCode 的值,它们只是不绑定。例如,实际创建窗格的代码是

public void AddGraph(string FullName, string ExchangeCode, string TickerCode)
    {
        var graphViewModel = new GraphViewModel(FullName, ExchangeCode, TickerCode);
        _panes.Add(graphViewModel);
        ActiveDocument = graphViewModel;

    }

在这里,每一步都有两个值。假设我添加了 5 个不同的窗格,它们都有正确的 ExchangeCode 和 TickerCode,但没有任何内容传递给自定义控件。

如果您需要有关绑定值的自定义控件的更多信息,请使用以下代码:Passing parameters to custom control (databinding)

备注:如您所见,我没有放太多代码,如果您认为可能有帮助,请提出请求,我会添加所需的内容。请注意,整个窗口管理器的全局逻辑与 AvalonDock 测试应用程序(AvalonDock.MVVMTestApp)中提供的相同。

【问题讨论】:

  • 我猜我使用的数据绑定方法有问题。如果取出与我当前问题无关的所有内容并创建一些空白项目,其 MainWindow 仅由自定义控件 Graph 和只有两个参数的 datacontext 组成,并将它们绑定到自定义控件:同样的问题,尽管参数实际上存在于数据上下文,但不传递给自定义控件。我去看看……

标签: c# wpf data-binding avalondock


【解决方案1】:

例如,如果我有 ChartView 和 ChartViewModel: 在 MainWindow.xaml 中:

    <xcad:DockingManager x:Name="dockingManager"
                         AnchorablesSource="{Binding Path=Anchorables}"
                         DocumentsSource="{Binding Path=Documents}"
                         ActiveContent="{Binding Path=ActiveDocument, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}">

        <xcad:DockingManager.LayoutItemTemplateSelector>
            <selfViewPane:PaneTemplateSelector>
                <selfViewPane:PaneTemplateSelector.ChartViewTemplate>
                    <DataTemplate>
                        <selfViewDocument:ChartView />
                    </DataTemplate>
                </selfViewPane:PaneTemplateSelector.ChartViewTemplate>
            </selfViewPane:PaneTemplateSelector>
        </xcad:DockingManager.LayoutItemTemplateSelector>

        <xcad:DockingManager.LayoutItemContainerStyleSelector>
            <selfViewPane:PaneStyleSelector>
                <selfViewPane:PaneStyleSelector.ChartViewStyle>
                    <Style TargetType="{x:Type xcad:LayoutItem}">
                        <Setter Property="Title" Value="{Binding Model.Title}"/>                                
                        <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}"/>
                        <Setter Property="IconSource" Value="{Binding Model.IconSource}"/>
                        <Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
                    </Style>
                </selfViewPane:PaneStyleSelector.ChartViewStyle>
            </selfViewPane:PaneStyleSelector>
        </xcad:DockingManager.LayoutItemContainerStyleSelector>

        <xcad:DockingManager.LayoutUpdateStrategy>
            <selfViewPane:LayoutInitializer />
        </xcad:DockingManager.LayoutUpdateStrategy>

        <xcad:LayoutRoot>
            <xcad:LayoutPanel Orientation="Horizontal">
                <xcad:LayoutAnchorablePane Name="ToolsPane" DockWidth="200">
                </xcad:LayoutAnchorablePane>
                <xcad:LayoutDocumentPane />
            </xcad:LayoutPanel>
        </xcad:LayoutRoot>                
    </xcad:DockingManager>

并且: 在 ChartViewModel 我有属性 ChartPlotModel:

/// <summary>
/// Gets or sets the ChartPlotModel.
/// </summary>
public PlotModel ChartPlotModel
{
    get
    {
        return this.chartPlotModel;
    }

    set
    {
        if (this.chartPlotModel != value)
        {
            this.chartPlotModel = value;
            this.RaisePropertyChanged("ChartPlotModel");
        }
    }
}

在 ChartView 中我可以绑定:

<UserControl x:Class="Jofta.Analyzer.UI.Classes.View.Document.ChartView"
             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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
             xmlns:oxy="http://oxyplot.org/wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <xctk:BusyIndicator IsBusy="{Binding Path=IsBusy}">
        <Grid>
            <oxy:PlotView Model="{Binding ChartPlotModel}" />
        </Grid>
    </xctk:BusyIndicator>

</UserControl>

在这个例子中,我从 oxyplot 绑定到 PlotView,但我认为,你可以使用这种模式。你有 GraphViewModel、GraphView 和 TickerCode 和 ExchangeCode。

【讨论】:

  • 除非我遗漏了什么,否则这并不能回答我的问题。我想我解释得不够清楚。据我了解,您所做的是在主窗口中有一个自定义控件,然后在此自定义控件中绑定数据。我的目标是将 MainWindow xaml 中的数据绑定到自定义控件,以便将一些参数传递给控件,​​这将根据这些参数将数据绑定到控件的 xaml(事实上,一个 oxyplot 图表:) )
  • 很抱歉我没有回答你的问题。我不确定我能理解你到底需要什么。在我的示例中,我有 MVVM 模式。从对象资源管理器树视图 (LayoutAnchorableItem) 我正在打开图表 (LayoutItem)。
  • 别对 Jofta 感到抱歉,尝试提供帮助非常好,非常好 :) 我现在正在尝试寻找一些关于数据绑定的尽可能通用的教程,我想我可以在变得更好后适应它。
  • 好的。如果您找到解决方案,请在此处发布……
猜你喜欢
  • 2013-08-29
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 2016-06-30
  • 2020-05-29
  • 1970-01-01
相关资源
最近更新 更多