列表框中的所有组合框在更改任何1个组合框时发生更改 [英] All ComboBoxes in a ListBox change when any 1 of them is changed
问题描述
我在绑定到自定义类型的 ObservableCollection
的表单上有一个 ListBox
。在每个项目中有一个 ComboBox
绑定到枚举类型。当窗口加载时,所有 ComboBox
es默认为某个值。当我为任何一个(从UI而不是代码)更改 SelectedItem
时,所有其他 ComboBox
SelectedItem
。
I have a ListBox
on a form that is bound to an ObservableCollection
of a custom type. Within each item there is a ComboBox
bound to an enumeration type. When the window loads, all ComboBox
es are defaulted to a certain value. When I change the SelectedItem
for any one (from the UI, not from code), all other ComboBox
es change to the same SelectedItem
.
我不知道我做了什么错,这里是我当前的XAML正在处理此问题。
I'm not sure what I've done wrong, here is my current XAML that is handling this.
<Window.Resources>
<ObjectDataProvider x:Key="SyncOperationValues"
MethodName="GetNames"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:SyncOperationEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
...
<DataTemplate x:Key="SyncListTemplate">
<Grid Grid.Column="1" Grid.RowSpan="2" Margin="0,0,20,0" x:Name="olDetails"
DataContext="{Binding Path=OlContact}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
...
<ComboBox x:Name="SyncOp"
Width="120" Height="19"
Margin="4,0,10,0"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource SyncOperationValues}}"
SelectedItem="{Binding Operation}"
VerticalAlignment="Center" />
...
和 ListBox
:
<ListBox x:Name="SyncList"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ItemContainerStyle="{StaticResource StretchedContainerStyle}"
ItemTemplate="{StaticResource SyncListTemplate}">
ListBox>
我尝试了一些不同的选项,比如绑定到 CollectionView
;然而没有什么似乎工作。
I have tried some different options, like binding to a CollectionView
; however nothing seems to work. Can anyone point me to my mistake?
谢谢!
推荐答案
我终于找到了一个解决方案。我最后写了一个ValueConverter的枚举类型。我一直以为这是没有必要的印象,但由于某种原因是,至少如果ComboBox是在另一个列表(ListBox在我的情况下)某种类型。
I have finally found a solution. I ended up writing a ValueConverter for the enumeration type. I'd been under the impression that this was not necessary, but for some reason it is, at least if the ComboBox is within another list (ListBox in my case) of some sort.
我确实需要将IsSynchronizedWithCurrentItem属性设置为false,因为John M建议,所以感谢John!这里是转换器代码,以防任何人需要做这样的事情。
I did need to set the IsSynchronizedWithCurrentItem property to false as John M suggested, so thanks to John for that! Here is the converter code in case anyone else needs to do something like this.
[ValueConversion( typeof( SyncOperationEnum ), typeof( String ) )]
public class SyncOperationConverter : IValueConverter {
#region IValueConverter Members
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
if( value != null && value.GetType() == typeof( SyncOperationEnum ) )
return Enum.GetName( typeof( SyncOperationEnum ), value );
return null;
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
if( value != null && targetType == typeof( SyncOperationEnum ) )
foreach( object enumValue in Enum.GetValues( targetType ) )
if( value.Equals( Enum.GetName( targetType, enumValue ) ) )
return enumValue;
return null;
}
#endregion
我的XAML现在看起来像this:
And my XAML now looks like this:
<Window.Resources>
<local:SyncOperationConverter x:Key="SyncConverter" />
<ObjectDataProvider x:Key="SyncOperationValues"
MethodName="GetNames"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:SyncOperationEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
...
<DataTemplate x:Key="SyncListTemplate">
<Grid Grid.Column="1" Grid.RowSpan="2" Margin="0,0,20,0" x:Name="olDetails"
DataContext="{Binding Path=OlContact}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
...
<ComboBox x:Name="SyncOp"
Width="120" Height="19"
Margin="4,0,10,0"
IsSynchronizedWithCurrentItem="False"
ItemsSource="{Binding Source={StaticResource SyncOperationValues}}"
SelectedValue="{Binding Path=Operation,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource SyncConverter}}"
VerticalAlignment="Center" />
这篇关于列表框中的所有组合框在更改任何1个组合框时发生更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!