WPF 用户控件依赖属性值

发布于 2024-12-10 01:25:54 字数 1701 浏览 1 评论 0原文

我有以下问题:我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白首有我共你 2024-12-17 01:25:54

您错误地定义了依赖属性的属性外观。在属性定义中,您需要处理依赖属性并设置和返回其值:

public int AgreementID
{
    get { return (int) GetValue(AgreementIDProperty ); }
    set { SetValue(AgreementIDProperty, value); }
}

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:

public int AgreementID
{
    get { return (int) GetValue(AgreementIDProperty ); }
    set { SetValue(AgreementIDProperty, value); }
}
骷髅 2024-12-17 01:25:54

如前所述,您的属性包装器应该在依赖项属性上调用 GetValueSetValue(并且您可以省去支持字段)。不过,值得指出的是,当直接在 XAML 中或通过声明性 Binding 设置 DependencyProperty 时,将绕过包装器属性。因此,如果您实际上使用 GetValue 检查 DependencyProperty 本身的值,那么您会看到该值实际上是由您的 设置的绑定。这是通过程序代码中的包装器检索属性值失败的原因,因为它没有访问 DependencyProperty

As already indicated, your property wrapper should be calling GetValue and SetValue 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 the DependencyProperty is set directly in XAML or from a declarative Binding. So if you actually checked the value of the DependencyProperty itself using GetValue then you would see that the value was actually being set by your Binding. It's the retrieval of the property value via your wrapper in procedural code that's failing since it's not accessing the DependencyProperty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文