WPF 用户控件依赖属性值
我有以下问题:我在 WPF 中创建了一个 UserControl,它有一个 ContractID 属性。我需要在另一个UserControl中调用这个UserControl,并且在调用它时我需要为其提供一个AgreementID。现在我在第一个 UserControl 中创建了 DependencyProperty,但它不起作用。
这是我的第一个UserControl的代码:
public partial class UC1001_AgreementDetails_View : UserControl
{
private UC1001_ActiveAgreementContract _contract;
private int _agreementID;
public int AgreementID
{
get { return _agreementID; }
set { _agreementID = value; }
}
public UC1001_AgreementDetails_View()
{
InitializeComponent();
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
这是我调用以前的UserControl的XAML:
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<!--<Setter Property="Text" Value="{Binding Months[9].AgreementID}"/>-->
<Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/>
<Setter Property="Background" Value="White" />
<Setter Property="DataGridCell.ToolTip">
<Setter.Value>
<my:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88" AgreementID="{Binding Months[9].AgreementID}"/>
</Setter.Value>
</Setter>
所以我想将那个月的AgreementID传递到第一个UserControl上,但是当我检查它时,传递的值总是0,但它应该是 3。我检查了 Months[9].AgreementID 值,确实是 3。所以我在绑定中得到了正确的值,但它没有正确传递。
我希望您能清楚我的问题,如果需要,可以提供其他信息!
I have the following problem: I made a UserControl in WPF which has an AgreementID property. I need to call this UserControl in another UserControl, and I need to give an AgreementID with it when I call it. Now I made a DependencyProperty in the first UserControl, but it does not work.
This is the code of my first UserControl:
public partial class UC1001_AgreementDetails_View : UserControl
{
private UC1001_ActiveAgreementContract _contract;
private int _agreementID;
public int AgreementID
{
get { return _agreementID; }
set { _agreementID = value; }
}
public UC1001_AgreementDetails_View()
{
InitializeComponent();
}
public static readonly DependencyProperty AgreementIDProperty = DependencyProperty.Register("AgreementID", typeof(int), typeof(UC1001_AgreementDetails_View), new PropertyMetadata(null));
This is the XAML where I call previous UserControl:
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<!--<Setter Property="Text" Value="{Binding Months[9].AgreementID}"/>-->
<Setter Property="Tag" Value="{Binding Months[9].AgreementID}"/>
<Setter Property="Background" Value="White" />
<Setter Property="DataGridCell.ToolTip">
<Setter.Value>
<my:UC1001_AgreementDetails_View Background="#FFF" Opacity="0.88" AgreementID="{Binding Months[9].AgreementID}"/>
</Setter.Value>
</Setter>
So I want to pas the AgreementID of that month onto the first UserControl, but when I check it, the passed value is always 0, but it should be 3. I checked the Months[9].AgreementID value and it was 3 indeed. So I get the right value in the binding, but it's not passing through right.
I hope my problem is a bit clear to you, additional information can be given if wanted!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您错误地定义了依赖属性的属性外观。在属性定义中,您需要处理依赖属性并设置和返回其值:
You are defining the Property facade for the Dependency Property wrong. In your property definition, you need to work on the Dependency Property and set and return its value:
如前所述,您的属性包装器应该在依赖项属性上调用
GetValue
和SetValue
(并且您可以省去支持字段)。不过,值得指出的是,当直接在 XAML 中或通过声明性Binding
设置DependencyProperty
时,将绕过包装器属性。因此,如果您实际上使用GetValue
检查DependencyProperty
本身的值,那么您会看到该值实际上是由您的设置的绑定
。这是通过程序代码中的包装器检索属性值失败的原因,因为它没有访问DependencyProperty
。As already indicated, your property wrapper should be calling
GetValue
andSetValue
on the dependency property (and you can dispense with the backing field). However, it is worth pointing out that the wrapper property will be bypassed when theDependencyProperty
is set directly in XAML or from a declarativeBinding
. So if you actually checked the value of theDependencyProperty
itself usingGetValue
then you would see that the value was actually being set by yourBinding
. It's the retrieval of the property value via your wrapper in procedural code that's failing since it's not accessing theDependencyProperty
.