WPF:如何在主窗口中使用用户控件的属性?

发布于 2025-02-04 16:23:09 字数 4367 浏览 2 评论 0原文

我制作了一个有效的用户控件,并希望在主窗口中多次使用它。它具有标签中显示的属性结果。在我的主窗口中,我有一个标签,该标签应显示所有结果的总和。

这是用户控件(它起作用,很重要,我想使用一个属性结果):

using System.ComponentModel;
using System.Windows;

namespace WpfApp2
{
    public partial class CardSelectionControl : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int Level
        {
            get { return (int)GetValue(LevelProperty); }
            set { SetValue(LevelProperty, value); }
        }
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public int Result
        {
            get { return (int)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }


        public static readonly DependencyProperty ResultProperty =
            DependencyProperty.Register("Result", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(20));

        public static readonly DependencyProperty LevelProperty =
            DependencyProperty.Register("Level", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(4, new PropertyChangedCallback(OnChanged)));

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(0));

        private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CardSelectionControl control = (CardSelectionControl)d;
            control.Recalculate();
        }

        public void Recalculate()
        {
            //calculates a new value for level
        }



        public CardSelectionControl()
        {
            DataContext = this;
            InitializeComponent();
        }
    }
}

此主窗口:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d"
        Title="MainWindow">
    <StackPanel x:Name="panel1">
        <local:CardSelectionControl x:Name="control1" Result="{Binding Costs1}" ></local:CardSelectionControl>
        <local:CardSelectionControl x:Name="control2" Result="{Binding Costs2}" ></local:CardSelectionControl>

        <Label Height="50" Content="{Binding TotalCosts}"></Label>
    </StackPanel>
</Window>

它的代码背后:

using System.ComponentModel;
using System.Windows;

namespace WpfApp2
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int Costs1
        {
            get { return (int)GetValue(CostsProperty); }
            set { SetValue(CostsProperty, value); }
        }

        public static readonly DependencyProperty CostsProperty =
            DependencyProperty.Register("Costs1", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(0));

        public int Costs2
        {
            get { return (int)GetValue(Costs2Property); }
            set { SetValue(Costs2Property, value); }
        }

        public static readonly DependencyProperty Costs2Property =
            DependencyProperty.Register("Costs2", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(0));



        public int TotalCosts
        {
            get { return (int)GetValue(TotalCostsProperty); }
            set { SetValue(TotalCostsProperty, value); }
        }

        public static readonly DependencyProperty TotalCostsProperty =
            DependencyProperty.Register("TotalCosts", typeof(int), typeof(MainWindow), new PropertyMetadata(0));



        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

我如何绑定到结果?成本1和成本2没有价值。 以及如何将这些绑定结合并将其结合到标签上?我考虑过创建一种方法calctotal()并为每个成本属性实现新的属性changedcallback,这是可能的吗?

I made a user control that works and want to use it multiple times in my main window. It has a property Result that is shown in a label. In my main window I have a label that should show the sum of all Results.

This is the user control (it works, just important there's a property Result I want to use):

using System.ComponentModel;
using System.Windows;

namespace WpfApp2
{
    public partial class CardSelectionControl : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int Level
        {
            get { return (int)GetValue(LevelProperty); }
            set { SetValue(LevelProperty, value); }
        }
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public int Result
        {
            get { return (int)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }


        public static readonly DependencyProperty ResultProperty =
            DependencyProperty.Register("Result", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(20));

        public static readonly DependencyProperty LevelProperty =
            DependencyProperty.Register("Level", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(4, new PropertyChangedCallback(OnChanged)));

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(0));

        private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CardSelectionControl control = (CardSelectionControl)d;
            control.Recalculate();
        }

        public void Recalculate()
        {
            //calculates a new value for level
        }



        public CardSelectionControl()
        {
            DataContext = this;
            InitializeComponent();
        }
    }
}

This main window:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d"
        Title="MainWindow">
    <StackPanel x:Name="panel1">
        <local:CardSelectionControl x:Name="control1" Result="{Binding Costs1}" ></local:CardSelectionControl>
        <local:CardSelectionControl x:Name="control2" Result="{Binding Costs2}" ></local:CardSelectionControl>

        <Label Height="50" Content="{Binding TotalCosts}"></Label>
    </StackPanel>
</Window>

and its code behind:

using System.ComponentModel;
using System.Windows;

namespace WpfApp2
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int Costs1
        {
            get { return (int)GetValue(CostsProperty); }
            set { SetValue(CostsProperty, value); }
        }

        public static readonly DependencyProperty CostsProperty =
            DependencyProperty.Register("Costs1", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(0));

        public int Costs2
        {
            get { return (int)GetValue(Costs2Property); }
            set { SetValue(Costs2Property, value); }
        }

        public static readonly DependencyProperty Costs2Property =
            DependencyProperty.Register("Costs2", typeof(int), typeof(CardSelectionControl), new PropertyMetadata(0));



        public int TotalCosts
        {
            get { return (int)GetValue(TotalCostsProperty); }
            set { SetValue(TotalCostsProperty, value); }
        }

        public static readonly DependencyProperty TotalCostsProperty =
            DependencyProperty.Register("TotalCosts", typeof(int), typeof(MainWindow), new PropertyMetadata(0));



        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

How do I bind to Result? Costs1 and Costs2 don't have a value.
And how do I combine these bindings and bind them to the label? I thought about creating a method calcTotal() and implement a new PropertyChangedCallback for each Cost Property, is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最笨的告白 2025-02-11 16:23:09

我认为您应该使用其他绑定:

Result="{Binding Costs1, RelativeSource={RelativeSource AncestorType={x:Type Window}}}

或使用x:name =“ mainwindow” window 元素和

Result="{Binding Costs1, ElementName=MainWindow}

I think you should be using a different binding:

Result="{Binding Costs1, RelativeSource={RelativeSource AncestorType={x:Type Window}}}

or uses x:Name="MainWindow" on the Window element and

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