当从ParentControl设置为UserControl时,绑定不工作

本文关键字:绑定 工作 UserControl ParentControl 设置 当从 | 更新日期: 2023-09-27 18:08:28

我有一个MainWindow,它有一个UserControl.

主窗口被绑定到一个ViewModel,它是它的DataContext。

现在使用MainWindowViewModel,我想在UserControl中绑定某些控件。

UserControl对于我想要绑定的每个元素都有一个DependencyProperty。

从MainWindow我绑定UserControl的DependencyProperties到MainWindowViewModel,但是绑定不起作用。

主窗口XAML:

<Grid >       
    <userControls:MultiSelectEnvironmentContextControl  EnvironmentVersions="{Binding EnvironmentVersions}"  x:Name="MultiSelectEnvironmentContextControl" Margin="10"  />       
</Grid>

用户控件XAML:

    <ComboBox Grid.Column="0" Grid.Row="0" x:Name="ComboBoxVersions"  SelectedIndex="0" Margin="10" ItemsSource="{Binding EnvironmentVersions}">
       <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Version " />
                    <TextBlock Text="{Binding}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox> 

UserControl Code behind:

public partial class MultiSelectEnvironmentContextControl 
{        
    public MultiSelectEnvironmentContextControl()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    public IEnumerable<string> EnvironmentVersions
    {
        get { return (IEnumerable<string>)GetValue(EnvironmentVersionsProperty); }
        set { SetValue(EnvironmentVersionsProperty, value); }
    }
    // Using a DependencyProperty as the backing store for EnvironmentVersions.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnvironmentVersionsProperty =
        DependencyProperty.Register("EnvironmentVersions", typeof(IEnumerable<string>), typeof(MultiSelectEnvironmentContextControl), new PropertyMetadata(default(IEnumerable<string>)));

}

主窗口视图模型:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using Geico.Applications.Business.CoverageTestHarness.DomainLayer;
namespace Geico.Applications.Business.CoverageTestHarness.UIWPF.ViewModels
{
    class TestRunnerControlViewModel: ViewModelBase
    {
        private DomainFacade domainFacade;

        /// <summary>
        /// Initializes a new instance of the <see cref="TestRunnerControlViewModel"/> class.
        /// </summary>
        public TestRunnerControlViewModel()
        {
            domainFacade = ((App) Application.Current).DomainFacade;
        }
        /// <summary>
        /// Gets the environment versions.
        /// </summary>
        /// <value>
        /// The environment versions.
        /// </value>
        public IEnumerable<string> EnvironmentVersions
        {
            get
            {
                return domainFacade.GetEnvironmentVersions();
            }
        }
    }
}

当从ParentControl设置为UserControl时,绑定不工作

您的错误是在usercontrol构造函数中设置了dataconcontect。删除

this.DataContext = this;

public void MultiSelectEnvironmentContextControl ()
{
    ...
}
下面是我的代码:MainWindow.xaml
<Window x:Class="WpfApplication8.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:userControls="clr-namespace:WpfApplication8">
<Grid >
    <userControls:UserControl1  EnvironmentVersions="{Binding EnvironmentVersions}"  x:Name="MultiSelectEnvironmentContextControl" Margin="10"  />
</Grid>

MainWindow.cs

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.Navigation;
using System.Windows.Shapes;
namespace WpfApplication8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.DataContext = new TestRunnerControlViewModel();
            InitializeComponent();
        }
    }
}

UserControl.cs

  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  namespace WpfApplication8
  {
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
    public IEnumerable<string> EnvironmentVersions
    {
        get { return (IEnumerable<string>)GetValue(EnvironmentVersionsProperty); }
        set { SetValue(EnvironmentVersionsProperty, value); }
    }
    // Using a DependencyProperty as the backing store for EnvironmentVersions.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnvironmentVersionsProperty =
        DependencyProperty.Register("EnvironmentVersions", typeof(IEnumerable<string>), typeof(UserControl1), new PropertyMetadata(default(IEnumerable<string>)));
    }
}

vm.cs

  using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  using System.Windows;
  namespace WpfApplication8
  {
    class TestRunnerControlViewModel 
    {
        //  private DomainFacade domainFacade;
        public TestRunnerControlViewModel()
        {
            //domainFacade = ((App)Application.Current).DomainFacade;
        }
        public IEnumerable<string> EnvironmentVersions
        {
            get
            {
                return new List<string>() { "test", "test2" };
            }
        }
    }
  }