WPF 嵌套用户控件绑定(bind)

标签 wpf xaml binding

我正在尝试将一个值从 Window 向下绑定(bind)到 UserControl 中的 UserControl。但是,出于某种原因,据我所知,内部 UserControl 甚至从未尝试绑定(bind)。

MainWindow.xaml

<Window x:Class="PdfExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:PdfExample">
<Grid>
    <my:FileSystemBrowser HorizontalAlignment="Left" x:Name="fileSystemBrowser1" VerticalAlignment="Top" Height="311" Width="417" RootPath="C:\TFS\AE.Web.ezHealthQuoter.Common\1_Dev\Shared\Pdfs" />
</Grid>

FileSystemBrowser.xaml

<UserControl x:Class="PdfExample.FileSystemBrowser"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" xmlns:my="clr-namespace:PdfExample">
<DockPanel>
    <my:FileSystemTree x:Name="fileSystemTree1" RootPath="{Binding Path=RootPath}" Width="150" />
    <ListBox DockPanel.Dock="Right" />
</DockPanel>

FileSystemBrowser.xaml.cs

    public partial class FileSystemBrowser : UserControl
{
    #region Static Members
    static FileSystemBrowser()
    {
        PropertyChangedCallback rootPathChangedCallback = new PropertyChangedCallback(OnRootPathChanged);
        PropertyMetadata metaData = new PropertyMetadata(rootPathChangedCallback);
        RootPathProperty = DependencyProperty.Register("RootPath", typeof(string), typeof(FileSystemBrowser), metaData);
    }

    static DependencyProperty RootPathProperty;

    public static void OnRootPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as FileSystemBrowser).RootPath = e.NewValue as string;
    }
    #endregion

    public string RootPath
    {
        get { return this.ViewModel.RootPath; }
        set { this.ViewModel.RootPath = value; }
    }

    public FileSystemBrowserViewModel ViewModel
    {
        get;
        protected set;
    }

    public FileSystemBrowser()
    {
        InitializeComponent();
        this.ViewModel = new FileSystemBrowserViewModel();
        this.DataContext = this.ViewModel;
    }
}

public class FileSystemBrowserViewModel : INotifyPropertyChanged
{
    private string _rootPath;
    public string RootPath
    {
        get { return _rootPath; }
        set { _rootPath = value; RaisePropertyChanged("RootPath"); }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

文件系统树.xaml

<UserControl x:Class="PdfExample.FileSystemTree"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<DockPanel>
    <TreeView SelectedValuePath="{Binding Path=SelectedValuePath, Mode=TwoWay}" HorizontalAlignment="Stretch" Name="treeView1" VerticalAlignment="Stretch" ItemsSource="{Binding RootFolder}" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" Margin="0">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Folders}">                    
                <TextBlock Text="{Binding FolderName}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</DockPanel>

FileSystemTree.xaml.cs

    public partial class FileSystemTree : UserControl, INotifyPropertyChanged
{
    #region Static Members

    static DependencyProperty RootPathProperty;

    static FileSystemTree()
    {
        PropertyChangedCallback rootPathChangedCallback = new PropertyChangedCallback(OnRootPathChanged);
        PropertyMetadata metaData = new PropertyMetadata(rootPathChangedCallback);
        RootPathProperty = DependencyProperty.Register("RootPath", typeof(string), typeof(FileSystemTree), metaData);
    }

    public static void OnRootPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as FileSystemTree).RootPath = e.NewValue as string;
    }

    #endregion

    public string RootPath
    {
        get { return this.ViewModel.RootPath; }
        set { this.ViewModel.RootPath = value; }
    }

    public FileSystemTreeViewModel ViewModel
    {
        get;
        protected set;
    }

    public FileSystemTree()
    {            
        InitializeComponent();
        this.ViewModel = new FileSystemTreeViewModel();
        this.DataContext = this.ViewModel;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

public class FileSystemTreeViewModel : INotifyPropertyChanged
{
    public IFolder[] RootFolder
    {
        get
        {
            if (RootPath != null)
                return new IFolder[] { new FileSystemFolder(RootPath) };

            return null;
        }
    }

    private string _rootPath;
    public string RootPath
    {
        get { return _rootPath; }
        set
        {
            _rootPath = value;
            RaisePropertyChanged("RootPath");
            RaisePropertyChanged("RootFolder");
        }
    }

    private string _selectedValuePath;
    protected string SelectedValuePath
    {
        get { return _selectedValuePath; }
        set { _selectedValuePath = value; }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

我知道树可以工作,因为如果我只是将树放在 MainWindow.xaml 中,就可以了。但出于某种原因,MainWindow.xaml 中的 RootPath 值被绑定(bind)到 FileSystemBrowser 并停在那里。它永远不会一直到 FileSystemTree。我错过了什么?

最佳答案

可以确定的信息很少,但我认为问题是未设置的 DataContext。尝试相对绑定(bind),这可能会有所帮助。在 FileSystemBrowser.xaml 中更改绑定(bind),如下所示:

<my:FileSystemTree x:Name="fileSystemTree1" 
    RootPath="{Binding Path=RootPath,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" 
    Width="150" />     

另一种可能性是将 UserControls this-reference 设置为 DataContext。这也应该有所帮助。

关于WPF 嵌套用户控件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4741178/

相关文章:

xaml - 在 MultiSelect ListView (UWP) 中隐藏默认复选框

wpf - 如何公开嵌套在 UserControl 中的控件的 DependencyProperty?

wpf - 相对源和弹出

c# - ListView WinForms 绑定(bind)到 ObservableCollection<T>

wpf - 托管的Winform控件不响应来自WPF的事件

c# - 为什么 System.Windows.FrameworkElement 对宽度和高度等属性使用 double 类型?

wpf - DevExpress GridControl 列自动宽度

c# - WPF MVVM 数据模型

xaml - Xamarin Forms 中是否有 'UpdateSourceTrigger' 等效项?

ios - RxSwift var-outlet 绑定(bind)组织