WPF 在 属性 更改时更新两个值
WPF update two values when a property is changed
我运行遇到以下问题 XAML:
<ListBox x:Name="lstRegion" ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate DataType="ListBoxItem">
<CheckBox Content="{Binding Element.Region_Code}" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
此代码将更新包含的 ListBoxItem 上的 IsSelected 属性,允许我 return 来自 lstRegion.SelectedItems 的此内容。
但是,当复选框 IsChecked 值更改时,我还需要更新 ItemsSource 中的值。
有没有办法更新 ItemsSource 和 ListBoxItem 中的值?看来我可以改变其中之一,但不能同时改变两者。
我确定我可以捕获 PropertyChanged 事件并手动更新值,但这似乎是我在做一个额外的步骤,因为我没有正确理解某些东西。感谢任何帮助。
这是用于填充 ListBox 上的 ItemsSource 的 class:
public class SelectionItem<T> : INotifyPropertyChanged
{
#region private fields
/// <summary>
/// indicates if the item is selected
/// </summary>
private bool _isSelected;
#endregion
public SelectionItem(T element, bool isSelected)
{
Element = element;
IsSelected = isSelected;
}
public SelectionItem(T element):this(element,false)
{
}
#region public properties
/// <summary>
/// this UI-aware indicates if the element is selected or not
/// </summary>
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (_isSelected != value)
{
_isSelected = value;
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
}
/// <summary>
/// the element itself
/// </summary>
public T Element { get; set; }
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
}
像这样的东西应该能满足您的需求:
<ListBox x:Name="lstRegion" ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate DataType="ListBoxItem">
<CheckBox Content="{Binding Element.Region_Code}" IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
我运行遇到以下问题 XAML:
<ListBox x:Name="lstRegion" ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate DataType="ListBoxItem">
<CheckBox Content="{Binding Element.Region_Code}" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
此代码将更新包含的 ListBoxItem 上的 IsSelected 属性,允许我 return 来自 lstRegion.SelectedItems 的此内容。
但是,当复选框 IsChecked 值更改时,我还需要更新 ItemsSource 中的值。 有没有办法更新 ItemsSource 和 ListBoxItem 中的值?看来我可以改变其中之一,但不能同时改变两者。 我确定我可以捕获 PropertyChanged 事件并手动更新值,但这似乎是我在做一个额外的步骤,因为我没有正确理解某些东西。感谢任何帮助。
这是用于填充 ListBox 上的 ItemsSource 的 class:
public class SelectionItem<T> : INotifyPropertyChanged
{
#region private fields
/// <summary>
/// indicates if the item is selected
/// </summary>
private bool _isSelected;
#endregion
public SelectionItem(T element, bool isSelected)
{
Element = element;
IsSelected = isSelected;
}
public SelectionItem(T element):this(element,false)
{
}
#region public properties
/// <summary>
/// this UI-aware indicates if the element is selected or not
/// </summary>
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (_isSelected != value)
{
_isSelected = value;
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
}
/// <summary>
/// the element itself
/// </summary>
public T Element { get; set; }
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
}
像这样的东西应该能满足您的需求:
<ListBox x:Name="lstRegion" ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate DataType="ListBoxItem">
<CheckBox Content="{Binding Element.Region_Code}" IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>