【问题标题】:C# WPF issue: cannot initialize a Combobox selected value correctlyC# WPF 问题:无法正确初始化组合框选定值
【发布时间】:2016-06-12 01:56:42
【问题描述】:

我的代码正在尝试为我自己的数据结构的对象列表动态创建组合框。每个对象都包含一个值列表,这些值将作为其项目填充到组合框中。该对象还包含一个名为“SelectedValue”的值。我希望每个组合框默认显示这个“SelectedValue”。但这不起作用。每次显示视图后,每个组合框仅将其第一项显示为选定值。

这是我的 XAML 代码:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListView ItemsSource="{Binding ViewItemList}" SelectionMode="Single" SelectedItem="{Binding Path=SelectedList}" HorizontalAlignment="Left" Height="218" Margin="44,30,0,0" VerticalAlignment="Top" Width="377">
        <ListView.View>
            <GridView>
                <GridViewColumn >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding ComboBoxItemList, Mode=OneWay}" SelectedValue="{Binding SelectedValue, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="5,0"  />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

这是后面的代码:

public class ComboboxViewModel : DependencyObject
{
    private ObservableCollection<ComboBoxItemsList> m_viewItemlist;
    public ObservableCollection<ComboBoxItemsList> ViewItemList
    {
        get
        {
            return m_viewItemlist;
        }
        set
        {
            m_viewItemlist = value;
        }
    }

    private ComboBoxItemsList m_selectedList;
    public ComboBoxItemsList SelectedList
    {
        get
        {
            return m_selectedList;
        }
        set
        {
            m_selectedList = value;
        }
    }

    public ComboboxViewModel(List<ComboBoxItemsList> ml)
    {
        m_viewItemlist = new ObservableCollection<ComboBoxItemsList>();
        foreach (ComboBoxItemsList item in ml)
        {
            m_viewItemlist.Add(item);
        }
        this.m_selectedList = ml[0];
    }
}

//a class to hold data structure to be shown in one combobox
public class ComboBoxItemsList : DependencyObject
{
    private List<string> m_comboBoxItemList;
    public List<string> ComboBoxItemList
    {
        get { return m_comboBoxItemList; }
    }

    private string m_selectedValue;
    public string SelectedValue
    {
        get { return (string)GetValue(SelectedItemProperty); }
        set
        {
            SetValue(SelectedItemProperty, (string)value);
        }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedValue", typeof(string), typeof(ComboBoxItemsList),
        new PropertyMetadata(OnSelectedValueChanged));

    private static void OnSelectedValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ComboBoxItemsList curModInfo = d as ComboBoxItemsList;
        if (curModInfo != null)
        {
            string oldVersion = e.OldValue as string;
            string newVersion = e.NewValue as string;
            curModInfo.SelectedValueChanged(oldVersion, newVersion);
        }
    }

    private void SelectedValueChanged(string oldValue, string newValue)
    {
        m_selectedValue = newValue;
    }

    public ComboBoxItemsList(List<string> list, string defaultVal)
    {
        m_comboBoxItemList = list;
        m_selectedValue = defaultVal;
    }
}


//This is main window to start the program
public MainWindow()
    {
        InitializeComponent();

        List<ComboBoxItemsList> mm = new List<ComboBoxItemsList>();
        List<string> nums = new List<string>();
        nums.Add("1");
        nums.Add("2");
        nums.Add("3");
        ComboBoxItemsList m1 = new ComboBoxItemsList(nums, "2");
        mm.Add(m1);
        List<string> letters = new List<string>();
        letters.Add("a");
        letters.Add("b");
        letters.Add("c");
        ComboBoxItemsList m2 = new ComboBoxItemsList(letters, "b");
        mm.Add(m2);
        ComboboxViewModel vm = new ComboboxViewModel(mm);
        this.DataContext = vm;
    }

【问题讨论】:

    标签: c# wpf xaml combobox


    【解决方案1】:

    您的代码看起来不错。您只需要在您的视图中删除 IsSynchronizedWithCurrentItem="True" 或将其设为 false 并在 ComboBoxItemsList 的构造函数中进行更改

     public ComboBoxItemsList(List<string> list, string defaultVal)
                {
                    m_comboBoxItemList = list;
                    SelectedValue = defaultVal;
                }
    

    【讨论】:

    • 如果它有效,请投票并接受答案
    • 完成,抱歉耽搁了。我只是想出了如何接受它作为答案:)。
    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多