如何绑定我的listview组合框的选定项目

时间:2012-08-30 23:10:09

标签: c# wpf mvvm-light

我对这个问题非常困惑 - 如果答案非常明显,我会道歉,但我对编程很陌生。

我将用户控件设置为视图,将其加载到主视图中的内容控件中。用户控件的数据文本(被调用的SetView)在MainView中设置。我很乐意绑定到SetView到UserControlVM(称为SetVM)。

在SetVM中,我加载了我创建的类的ObservableCollection:

    public class WeightSet : Weights
{


    public string BodyArea { get; set; }
    public string ExerciseType { get; set; }
    public int SetNumber { get; set; }
    public static ObservableCollection<int> Reps { get; set; }


    #region Constructor


    //This is the main constructor
    public WeightSet(string bodyarea, string exerciseType, int setNumber)
    {
        BodyArea = bodyarea;
        ExerciseType = exerciseType;
        SetNumber = setNumber;
        Reps = new ObservableCollection<int>();
        AddReps();

    }

    #endregion Constructor

    #region Methods

    public void AddReps()
    {
        for (int i = 1; i < 100; i++)
        {
            Reps.Add(i);
        }
    }
    #endregion Methods

我的SetView有一个ListView,其ItemsSource是

public ObservableCollection<WeightSet> Sets

这是ListView的xaml:

<UserControl x:Class="CalendarTest.SetView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:forcombo="clr-namespace:CalendarTest.Model.Repository.Local_Data"
         xmlns:VM="clr-namespace:CalendarTest.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="165" d:DesignWidth="300">
<Grid >
    <StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="{Binding CurrentExercise}" Width="100" Height="40"></Label>
        <Label Content="{Binding BodyArea}" Width="100" Height="40"></Label>

    </StackPanel>
        <ListView ItemsSource="{Binding Sets}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Set Number" DisplayMemberBinding="{Binding Path=SetNumber}" Width="100"></GridViewColumn>
                    <GridViewColumn Header="Select Reps" Width="120">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Width="100" ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}" ></ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Reps}"></GridViewColumn>
                </GridView>
            </ListView.View>


        </ListView>
    </StackPanel>

</Grid>

当我加载SetView时,我有一组设置数字和一个带静态列表的组合框。这是一个镜头:

enter image description here

我似乎无法将comboBox的selecteditem绑定回我的ViewModel。有没有办法在WeightSet类中设置所选项?我需要能够保存所选的号码吗?

我知道我不能绑定到属性,因为ComboBoxs的数量将由用户决定?我非常感谢您对我当前设计的任何提示或更正

2 个答案:

答案 0 :(得分:1)

您可以绑定组合框上的SelectedItem属性,并在WeightSet类中添加dependencyproperty

XAML:

<ComboBox ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}"
          SelectedItem="{Binding SelectedItem}" />

和属性

public static readonly DependencyProperty SelectedItemProperty =
    DependencyProperty.Register("SelectedItem", typeof (int), typeof (WeightSet), new PropertyMetadata(default(int)));

public int SelectedItem {
    get { return (int) GetValue(SelectedItemProperty); }
    set { SetValue(SelectedItemProperty, value); }
}

当在组合框中选择值时,将更新集合实例中的SelectedItem

答案 1 :(得分:0)

这里的关键是将组合框的SelectedItem依赖属性绑定到视图模型中的合适属性

首先,您需要在Weightset类中实现INotifyPropertyChanged

接下来在WeightClass中引入一个名为say SelectedRep的属性,它看起来像

int _selectedRep;
///<summary>Gets or sets SelectedRep.</summary>            
public int SelectedRep
{
    get { return _selectedRep; }
    set { _selectedRep = value; OnPropertyChanged("SelectedRep"); }
}

最后修改Xaml以将ComboBox的SelectedItem绑定到SelectedRep属性。

<ComboBox Width="100" ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}"  SelectedItem="{Binding SelectedRep, Mode=TwoWay}" />