這個問題在這里已經有了答案: DependencyProperty 系結問題 3 個答案 6 天前關閉。
我制作了一個有效的用戶控制元件,并希望在我的主視窗中多次使用它。它有一個顯示在標簽中的屬性 Result。在我的主視窗中,我有一個標簽,應該顯示所有結果的總和。
這是用戶控制元件(它有效,重要的是我要使用一個屬性 Result):
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;
}
}
}
如何系結到結果?Costs1 和 Costs2 沒有值。以及如何組合這些系結并將它們系結到標簽?我想過創建一個方法 calcTotal() 并為每個成本屬性實作一個新的 PropertyChangedCallback,這可能嗎?
uj5u.com熱心網友回復:
我認為您應該使用不同的系結:
Result="{Binding Costs1, RelativeSource={RelativeSource AncestorType={x:Type Window}}}
或用于x:Name="MainWindow"元素Window和
Result="{Binding Costs1, ElementName=MainWindow}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489040.html
