【发布时间】:2011-10-14 14:03:33
【问题描述】:
我有以下问题:我在 WPF 中创建了一个 UserControl,它有一个 AgreementID 属性。我需要在另一个 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确实。所以我在绑定中得到了正确的值,但它没有通过正确的传递。
希望我的问题对你来说有点清楚,如果需要可以提供更多信息!
【问题讨论】:
-
看看MVVM;这将允许您将数据存储在可以在您的视图(用户控件)之间共享的通用视图模型中。更多的时候不是 UserControl 是 WinForms 思维的遗留物。
标签: c# wpf xaml dependency-properties