ListBox中的所有组合框都会在更改其中任何一个时更改

时间:2009-02-06 20:00:38

标签: wpf data-binding combobox listbox

我在表单上有一个ListBox,该表单绑定到自定义类型的ObservableCollection。在每个项目中,有ComboBox绑定到枚举类型。当窗口加载时,所有ComboBox es默认为某个值。当我更改任何一个SelectedItem时(来自用户界面而不是代码),所有其他ComboBox更改为相同的SelectedItem

我不确定我做错了什么,这是我目前处理此问题的XAML。

<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;但似乎没有任何作用。有人能指出我的错误吗?

谢谢!

3 个答案:

答案 0 :(得分:7)

我有类似的情况,并将ComboBox上的IsSynchronizedWithCurrentItem属性设置为“False”修复它。我理解它的方式,将值设置为“True”意味着ComboBox值将是ListBox的当前项的同步值。基本上,所有ComboBox都绑定到相同的值。这听起来就像你正在经历的那样。尝试:

IsSynchronizedWithCurrentItem="False"

答案 1 :(得分:4)

我终于找到了解决方案。我最终为枚举类型编写了一个ValueConverter。我的印象是这不是必要的,但出于某种原因,至少如果ComboBox在某种类型的另一个列表(在我的情况下是ListBox)中。

我确实需要像John M建议的那样将IsSynchronizedWithCurrentItem属性设置为false,所以感谢John为此!这是转换器代码,以防其他任何人需要做这样的事情。

[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现在看起来像这样:

<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" />

答案 2 :(得分:-1)

看起来你的“Operation”属性应该是一个静态属性。由于它在您更改时绑定到每个ComboBox, 所以其他一切都在你的XAML中,只需让属性像bellow

    static SyncOperationEnum _operation;

    public static SyncOperationEnum Operation
    {
        get { return _operation; }
        set { _operation = value;}
    }
相关问题