【问题标题】:1 Control, two separate binding sources1 控件,两个独立的绑定源
【发布时间】:2021-06-07 10:43:19
【问题描述】:

早安!

我有一个 WPF 应用程序,它将根据它收到的命令行参数显示许多不同的文件类型。它工作正常,但我想回去重构它。我做开发人员才几年,想掌握 MVVM。

我正在使用一个名为 Stylet 的 MVVM 设计包。在我的 PDF 视图中,我使用 Telerik RadPdfViewer 控件,Telerik 为您内置了所有这些绑定内容。例如,我使用它们预先配置的命令绑定将右键单击上下文菜单与命令“全选”和“复制”绑定。

我想将“文档源”属性绑定到我的视图模型,这样我就可以传入我想要加载的文档的路径。但是,控件的 DataContext 绑定到 Telerik 的 CommandDescriptors 阻止绑定到我的视图模型。

<telerik:RadPdfViewer x:Name="radPdfViewer" Grid.Row="1" 
                          DataContext="{Binding CommandDescriptors, ElementName=radPdfViewer}" 
                          DocumentSource="{Binding PDFDoc}" 
                          telerik:RadPdfViewerAttachedComponents.RegisterFindDialog="True" 
                          HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" 
                          telerik:StyleManager.Theme="Office_Black" Grid.ColumnSpan="2">
        <telerik:RadContextMenu.ContextMenu>
            <telerik:RadContextMenu>
                <telerik:RadMenuItem Header="Select All" 
                                     Command="{Binding SelectAllCommandDescriptor.Command}" />
                <telerik:RadMenuItem Header="Copy" 
                                     Command="{Binding CopyCommandDescriptor.Command}" />
            </telerik:RadContextMenu>
        </telerik:RadContextMenu.ContextMenu>
    </telerik:RadPdfViewer>

public class PDFViewModel
{
    private string _pdfDoc;
    public string PDFDoc
    {
        get
        {
            return _pdfDoc;
        }
        set
        {
            _pdfDoc = value;
        }
    }
    public PDFViewModel()
    {
        PDFDoc = @"t:\share\large.pdf";
    }
}

我看到两个选择

  1. 我打破了 Telerik 的预构建命令绑定,并弄清楚如何将全选和复制功能带入我的视图模型。

  2. Stylet 有一个 s:Action 函数,我可以在其中调用一个方法,在该函数中我可以使用 C# 将文档加载到 RadPdfViewer 控件中。我需要以某种方式在我的视图模型的方法中控制 gui 控件,但我不知道该怎么做。

有没有更好的方法?非常感谢您朝正确的方向轻推。

【问题讨论】:

  • 此控件是否在您有权访问的容器中?窗户还是其他?我的想法是您可以将视图模型绑定到父控件,然后使用相对或元素名称绑定将DocumentSource 绑定到父控件的DataContext。但我不确定你在做什么,所以很难说。
  • 感谢您的回复。父控件是用户控件。这有自己的视图模型。不确定这是否重要,但我使用的是 ViewModel first MVVM 模式。所以用户控件实际上是从带有导体的根视图模型中调用的。根视图有一个窗口。所以理论上,用户控件的数据上下文应该已经设置为视图模型。但是让我试试这个。

标签: c# wpf mvvm stylet


【解决方案1】:

Jason Tyler 的回复让我朝着正确的方向前进。谢谢!

所以因为我使用的是 ViewModel 优先模式,所以我不需要像我想的那样指定用户控件的 DataContext...它已经设置好了。

但是,他建议使用相对源进行绑定并研究如何做到这一点(我以前从未使用过 RelativeSource..我对这个东西有点陌生)我遇到了这个 Stack 帖子

How do I use WPF bindings with RelativeSource?

A Jeff Knight 发布了祖先绑定如何工作的图表。

使用它,我能够弄清楚语法,我的文档就出现了,我仍然可以使用绑定到 Telerik 的右键单击上下文菜单项。所以现在我的 Xaml 看起来像这样,说明文档源绑定发生了怎样的变化。

<telerik:RadPdfViewer x:Name="radPdfViewer" Grid.Row="1" 
                          DataContext="{Binding CommandDescriptors, ElementName=radPdfViewer}" 
                          DocumentSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.PDFDoc}" 
                          telerik:RadPdfViewerAttachedComponents.RegisterFindDialog="True" 
                          HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" 
                          telerik:StyleManager.Theme="Office_Black" Grid.ColumnSpan="2">
        <telerik:RadContextMenu.ContextMenu>
            <telerik:RadContextMenu>
                <telerik:RadMenuItem Header="Select All" 
                                     Command="{Binding SelectAllCommandDescriptor.Command}" />
                <telerik:RadMenuItem Header="Copy" 
                                     Command="{Binding CopyCommandDescriptor.Command}" />
            </telerik:RadContextMenu>
        </telerik:RadContextMenu.ContextMenu>
    </telerik:RadPdfViewer>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多