在 WPF 控件上实现 SelectedValues 属性
我正在创建一个 CheckedComboBox WPF 控件。 我想添加一个可以通过 XAML 绑定的 SelectedValuesProperty。 我尝试了一些不同的方法,但还无法让它发挥作用。 有人对如何解决这个问题有什么建议吗? 我的控件继承自MultiSelector。 提前致谢!
这是我到目前为止所拥有的,问题是我无法从对象中获取 itemcontainer:
public static readonly DependencyProperty SelectedValuesProperty = DependencyProperty.Register(
"SelectedValues", typeof( IEnumerable ), typeof( CheckedComboBox ),
new FrameworkPropertyMetadata( ( IEnumerable ) null,
new PropertyChangedCallback( OnSelectedValuesChanged ) ) );
private static void OnSelectedValuesChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
CheckedComboBox combo = ( CheckedComboBox ) d;
IEnumerable oldValue = ( IEnumerable ) e.OldValue;
IEnumerable newValue = ( IEnumerable ) e.NewValue;
// unselect all the old vlaues
if ( oldValue != null )
{
foreach ( object obj in oldValue )
{
CheckedComboBoxItem item = obj as CheckedComboBoxItem;
if ( item == null )
item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
if ( item != null && item.IsEnabled && item.IsSelected )
item.IsSelected = false;
}
}
// select all the new values
if ( e.NewValue != null )
{
foreach ( object obj in newValue )
{
CheckedComboBoxItem item = obj as CheckedComboBoxItem;
if ( item == null )
item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
if ( item != null && item.IsEnabled && !item.IsSelected )
item.IsSelected = true;
}
}
}
I'm working on creating a CheckedComboBox WPF control. I want to add a SelectedValuesProperty that I could bind through via XAML. I've tried a few different things and haven't been able to get it to work yet. Does anyone have any suggestions on how to approach this? My control inherits from MultiSelector. Thanks in advance!
This is what I have so far, problem is I can't get the itemcontainer from the object:
public static readonly DependencyProperty SelectedValuesProperty = DependencyProperty.Register(
"SelectedValues", typeof( IEnumerable ), typeof( CheckedComboBox ),
new FrameworkPropertyMetadata( ( IEnumerable ) null,
new PropertyChangedCallback( OnSelectedValuesChanged ) ) );
private static void OnSelectedValuesChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
CheckedComboBox combo = ( CheckedComboBox ) d;
IEnumerable oldValue = ( IEnumerable ) e.OldValue;
IEnumerable newValue = ( IEnumerable ) e.NewValue;
// unselect all the old vlaues
if ( oldValue != null )
{
foreach ( object obj in oldValue )
{
CheckedComboBoxItem item = obj as CheckedComboBoxItem;
if ( item == null )
item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
if ( item != null && item.IsEnabled && item.IsSelected )
item.IsSelected = false;
}
}
// select all the new values
if ( e.NewValue != null )
{
foreach ( object obj in newValue )
{
CheckedComboBoxItem item = obj as CheckedComboBoxItem;
if ( item == null )
item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
if ( item != null && item.IsEnabled && !item.IsSelected )
item.IsSelected = true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在尝试解决同样的问题。 我需要一个 SelectedValues (不是 SelectedItems)与 SelectedValuePath 结合使用,这样如果我传入对象集合,我可以在这些对象上指定一个属性以使用返回的值。 SelectedValues 将返回值的集合。
I'm trying to figure out this same problem. I need a SelectedValues (not SelectedItems) to use in conjunction with the SelectedValuePath so that if I pass in a Collection of objects, I can specify a property on those objects to use a the values returned. SelectedValues would return a collection of the values.