Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取“我的listview”组合框的“绑定选定项”';s_C#_Wpf_Mvvm Light - Fatal编程技术网

C# 如何获取“我的listview”组合框的“绑定选定项”';s

C# 如何获取“我的listview”组合框的“绑定选定项”';s,c#,wpf,mvvm-light,C#,Wpf,Mvvm Light,在这一点上我感到非常困惑——如果答案很明显,我表示歉意,但我对编程非常陌生 我将用户控件设置为视图,该视图加载到主视图中的内容控件中。usercontrol(称为SetView)的datacontext在MainView中设置。我可以很高兴地将SetView绑定到UserControlVM(称为SetVM) 在SetVM中,我加载了我创建的类的ObservableCollection: public class WeightSet : Weights { public str

在这一点上我感到非常困惑——如果答案很明显,我表示歉意,但我对编程非常陌生

我将用户控件设置为视图,该视图加载到主视图中的内容控件中。usercontrol(称为SetView)的datacontext在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
公共类权重集:权重
{
公共字符串BodyArea{get;set;}
公共字符串ExerciseType{get;set;}
public int SetNumber{get;set;}
公共静态ObservableCollection Reps{get;set;}
#区域构造函数
//这是主构造函数
公共权重集(字符串bodyarea、字符串exerciseType、int setNumber)
{
车身面积=车身面积;
ExerciseType=ExerciseType;
SetNumber=SetNumber;
Reps=新的ObservableCollection();
AddReps();
}
#端域构造函数
#区域方法
public void AddReps()
{
对于(int i=1;i<100;i++)
{
增加(i)项;
}
}
#端域法
然后,我的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时,我有我的集合编号列表和一个带有staticlist的组合框。下面是一个快照:

我似乎无法将组合框的selecteditem绑定回我的ViewModel。是否有方法在WeightSet类中设置所选项目?我需要能够保存所选数字


我知道我不能只绑定到属性,因为combobox的数量将由用户决定?如果您对我当前的设计有任何提示或更正,我将不胜感激

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

xaml:


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

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

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

接下来在WeightClass中引入一个名为say SelectedRep的属性

int _selectedRep;
///<summary>Gets or sets SelectedRep.</summary>            
public int SelectedRep
{
    get { return _selectedRep; }
    set { _selectedRep = value; OnPropertyChanged("SelectedRep"); }
}
int\u选择的步骤;
///获取或设置SelectedRep。
公共int-SelectedRep
{
获取{return\u selectedRep;}
设置{u selectedRep=value;OnPropertyChanged(“selectedRep”);}
}
最后修改Xaml以将组合框的SelectedItem绑定到SelectedRep属性

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


Perfect-非常感谢我所需要的。现在,我在虚拟机中收集的权重集最终得到了一个关于用户选择的代表图,这正是我想要的。看起来我需要更多地了解依赖项属性!!@Luthervd:是的,记住你需要扩展DependencyObject才能调用GetValue和SetValue方法。您可以通过创建一个新类
WeightSetViewModel
来实现这一点,并让该方法“反映”您的
WeightSet
model类。您的视图与viewmodel对话,并且viewmodel在更新后将信息提供给模型。这样,您可以在viewmodel中拥有一组“显示”属性,以避免“污染”你的模型。如果你想让我在答案上展开,请写一条评论。如果我只有一个ComboBox,我会这样做(尽管我使用的是MVVM light)-但是它是用户定义的ComboBox数,因此SelectedRep需要在加载到ViewModel的每个WeightSet对象中结束
int _selectedRep;
///<summary>Gets or sets SelectedRep.</summary>            
public int SelectedRep
{
    get { return _selectedRep; }
    set { _selectedRep = value; OnPropertyChanged("SelectedRep"); }
}
<ComboBox Width="100" ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}"  SelectedItem="{Binding SelectedRep, Mode=TwoWay}" />