2017-03-23 42 views
-1

我有一个包含wpf用户控件的dll。如何从wpf用户控件设置公共变量值?

我在另一个wpf项目中有一个wpf窗口,其中包含上述用户控件。

我在wpf用户控件中有两个公共属性。

我想从添加wpf用户控件的wpf窗口中设置这些属性。

我试图做使用依赖属性,如下所示:

TestUserControl.xaml: -

<UserControl x:Class="TestDependencyProperty.TestUserControl" 
      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"> 
    <Grid> 
     <Label Content="test property" x:Name="lblTestProperty"/> 
    </Grid> 
</UserControl> 

TestUserControl.xaml.cs: -

using System.ComponentModel; 
using System.Windows; 

namespace TestDependencyProperty 
{ 
    /// <summary> 
    /// Interaction logic for TestUserControl.xaml 
    /// </summary> 
    public partial class TestUserControl 
    { 
     public TestUserControl() 
     { 
      InitializeComponent(); 
      SetLabelText(); 
     } 

     private void SetLabelText() 
     { 
      lblTestProperty.Content = TestProperty; 
     } 

     public static readonly DependencyProperty TestDependencyProperty = 
      DependencyProperty.Register("TestProperty", 
       typeof(string), 
       typeof(TestUserControl)); 

     [Bindable(true)] 
     public string TestProperty 
     { 
      get 
      { 
       return (string)this.GetValue(TestDependencyProperty); 
      } 
      set 
      { 
       this.SetValue(TestDependencyProperty, value); 
      } 
     } 
    } 

MainWindow.xaml : -

<Window x:Class="TestDependencyProperty.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:local="clr-namespace:TestDependencyProperty" 
     > 
    <Grid> 
     <local:TestUserControl x:Name="ucTest" TestProperty="HelloWorld"/> 
    </Grid> 
</Window> 

我在期待内容为“HelloWorld”的标签。

那么有人可以告诉我该怎么做吗?

谢谢。

+1

你尝试了什么?你有没有在WPF中设置过一个属性?你上次怎么做?你为什么认为这种情况不同?你在这里有任何具体的具体问题吗? –

+0

你使用了依赖属性吗? –

+0

@Ed Plunkett感谢您的回复,我从未尝试过从用户控件属性绑定公共变量。如果您有关于此问题的任何代码段,请转发。谢谢。 – pankaj

回答

0

用户控制XAML:

<UserControl x:Class="TestDependencyProperty.TestUserControl" 
      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:local="clr-namespace:WpfApplication3" 
      DataContext="{Binding RelativeSource={RelativeSource Self}}" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Label Content="{Binding TestProperty}" x:Name="lblTestProperty"/> 
    </Grid> 
</UserControl> 

用户控制代码:

public partial class TestUserControl : UserControl 
    { 
     public TestUserControl() 
     { 
      InitializeComponent(); 
     } 

     public static readonly DependencyProperty TestDependencyProperty = 
      DependencyProperty.Register("TestProperty", 
       typeof(string), 
       typeof(TestUserControl)); 

     [Bindable(true)] 
     public string TestProperty 
     { 
      get 
      { 
       return (string)this.GetValue(TestDependencyProperty); 
      } 
      set 
      { 
       this.SetValue(TestDependencyProperty, value); 
      } 
     } 
} 

你不需要SetLabelText();

MyNewUserControl.TestProperty="New Value"; 

依赖属性有变更通知内置于他们,所以一旦属性被绑定到他们将自动获得:

窗口主机用户控制

<local:TestUserControl TestProperty="Test Text" x:Name="MyNewUserControl" /> 
后面如果需要

代码当物业确实更新时更新。