如何知道 UserControl 中的哪个元素更改了 属性?
How to know which element inside UserControl changed the property?
UserControl 包含一些带有 "State" 属性
的自定义控件
<UserControl x:Class="MyNamespace.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="133" d:DesignWidth="175"
x:Name="my_user_control">
<Canvas>
<c:Led State="{Binding SegmentState, Mode=TwoWay}"/>
<c:Led State="{Binding SegmentState, Mode=TwoWay}"/>
<c:Led State="{Binding SegmentState, Mode=TwoWay}"/>
</Canvas>
SegmentState 是 属性 与 PropertyChangedCallback
的依赖项
public static DependencyProperty SegmentStateProperty =
DependencyProperty.Register("SegmentState", typeof(bool), typeof(MyUserControl),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(SegmentStateChanged)));
但在 SegmentStateChanged 中,我无法找到 LED 更改了哪个 属性。有什么办法可以查出来吗?
如果 SegmentState
将成为 bool
,您应该编写三个 SegmentState
属性并绑定每个 c:Led
控件的 State
属性到另一个:
<c:Led State="{Binding Segment0State, Mode=TwoWay}"/>
<c:Led State="{Binding Segment1State, Mode=TwoWay}"/>
<c:Led State="{Binding Segment2State, Mode=TwoWay}"/>
如果段数不固定,您可以编写一个简单的 SegmentState
class,它只有一个 bool Value
属性,有一个实例集合称为 SegmentStates
,并绑定到该集合中的项目——但随后您必须编写一些代码来响应 Value
属性中的更改。上面的方法涉及一点复制粘贴,但它是迄今为止最容易正确工作的方法。
<ItemsControl ItemsSource="{Binding SegmentStates}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<c:Led State="{Binding Value, Mode=TwoWay}"/>
<DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
UserControl 包含一些带有 "State" 属性
的自定义控件<UserControl x:Class="MyNamespace.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="133" d:DesignWidth="175"
x:Name="my_user_control">
<Canvas>
<c:Led State="{Binding SegmentState, Mode=TwoWay}"/>
<c:Led State="{Binding SegmentState, Mode=TwoWay}"/>
<c:Led State="{Binding SegmentState, Mode=TwoWay}"/>
</Canvas>
SegmentState 是 属性 与 PropertyChangedCallback
的依赖项public static DependencyProperty SegmentStateProperty =
DependencyProperty.Register("SegmentState", typeof(bool), typeof(MyUserControl),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(SegmentStateChanged)));
但在 SegmentStateChanged 中,我无法找到 LED 更改了哪个 属性。有什么办法可以查出来吗?
如果 SegmentState
将成为 bool
,您应该编写三个 SegmentState
属性并绑定每个 c:Led
控件的 State
属性到另一个:
<c:Led State="{Binding Segment0State, Mode=TwoWay}"/>
<c:Led State="{Binding Segment1State, Mode=TwoWay}"/>
<c:Led State="{Binding Segment2State, Mode=TwoWay}"/>
如果段数不固定,您可以编写一个简单的 SegmentState
class,它只有一个 bool Value
属性,有一个实例集合称为 SegmentStates
,并绑定到该集合中的项目——但随后您必须编写一些代码来响应 Value
属性中的更改。上面的方法涉及一点复制粘贴,但它是迄今为止最容易正确工作的方法。
<ItemsControl ItemsSource="{Binding SegmentStates}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<c:Led State="{Binding Value, Mode=TwoWay}"/>
<DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>