C# WPF UserControl依赖项属性值
我有以下问题:我在WPF中创建了一个具有AgreementID属性的UserControl。我需要在另一个UserControl中调用这个UserControl,并且在调用它时需要给它一个AgreementID。现在,我在第一个UserControl中创建了DependencyProperty,但它不起作用 这是我的第一个UserControl的代码:C# WPF UserControl依赖项属性值,c#,wpf,xaml,dependency-properties,C#,Wpf,Xaml,Dependency Properties,我有以下问题:我在WPF中创建了一个具有AgreementID属性的UserControl。我需要在另一个UserControl中调用这个UserControl,并且在调用它时需要给它一个AgreementID。现在,我在第一个UserControl中创建了DependencyProperty,但它不起作用 这是我的第一个UserControl的代码: public partial class UC1001_AgreementDetails_View : 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));
<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>
这是我调用previous UserControl的XAML:
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));
<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 pas到第一个UserControl上,但是当我检查它时,传递的值总是0,但应该是3。我检查了月份[9]。AgreementID值,结果确实是3。所以我在绑定中得到了正确的值,但它没有正确地传递
我希望我的问题对您来说有点清楚,如果需要,可以提供更多信息 您为依赖项属性定义的属性facade错误。在属性定义中,您需要处理Dependency属性并设置和返回其值:
public int AgreementID
{
get { return (int) GetValue(AgreementIDProperty ); }
set { SetValue(AgreementIDProperty, value); }
}
如前所述,属性包装器应该调用依赖属性上的GetValue和SetValue,并且可以省去backing字段。但是,值得指出的是,当DependencyProperty直接在XAML中设置或从声明性绑定设置时,将绕过包装器属性。因此,如果您实际使用GetValue检查DependencyProperty本身的值,那么您将看到该值实际上是由绑定设置的。通过过程代码中的包装器检索属性值失败,因为它没有访问DependencyProperty。看看MVVM;这将允许您将数据存储在公共ViewModel中,该模型可以在视图和用户控件之间共享。通常情况下,用户控件不是WinForms思维的产物。