C# 如何从wpf用户控件设置公共变量值?
我有一个包含wpf用户控件的dll 我在另一个wpf项目中有一个wpf窗口,其中包含上述用户控件 我在wpf用户控件中有两个公共属性 我想从添加了wpf用户控件的wpf窗口中设置这些属性。 我已尝试使用dependency属性执行此操作,如下所示: TestUserControl.xaml:-C# 如何从wpf用户控件设置公共变量值?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个包含wpf用户控件的dll 我在另一个wpf项目中有一个wpf窗口,其中包含上述用户控件 我在wpf用户控件中有两个公共属性 我想从添加了wpf用户控件的wpf窗口中设置这些属性。 我已尝试使用dependency属性执行此操作,如下所示: TestUserControl.xaml:- <UserControl x:Class="TestDependencyProperty.TestUserControl" xmlns="http://schemas.mi
<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);
}
}
}
使用System.ComponentModel;
使用System.Windows;
命名空间TestDependencyProperty
{
///
///TestUserControl.xaml的交互逻辑
///
公共部分类TestUserControl
{
公共TestUserControl()
{
初始化组件();
SetLabelText();
}
私有void SetLabelText()
{
lblTestProperty.Content=TestProperty;
}
公共静态只读DependencyProperty TestDependencyProperty=
DependencyProperty.Register(“TestProperty”,
类型(字符串),
typeof(TestUserControl));
[可装订(真实)]
公共字符串TestProperty
{
得到
{
返回(字符串)this.GetValue(TestDependencyProperty);
}
设置
{
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”的标签
有人能告诉我怎么做吗
谢谢。用户控制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();
窗口宿主用户控件
<local:TestUserControl TestProperty="Test Text" x:Name="MyNewUserControl" />
依赖项属性具有内置的更改通知,因此一旦属性绑定到它们,它将在属性绑定时自动更新。您尝试了什么?您以前是否在WPF中设置过属性?上次是如何设置的?为什么您认为这种情况不同?您是否有任何具体的问题?Did您使用依赖属性了吗?@Ed Plunkett谢谢您的回复,我以前没有尝试过从用户控件属性绑定公共变量。如果您有关于此问题的任何代码段,请转发。谢谢。@Kelly Barnard嗨Kelly,我尝试过依赖属性,但依赖属性总是返回null,所以此问题也是“dependency property returns null”有什么解决方案吗?。谢谢:-)@pankaj你能展示一下你是如何实现你的dependency属性的吗
MyNewUserControl.TestProperty="New Value";