如何绑定ListView值取决于组合框选择的项目? [英] How To bind ListView values depends on combobox selected item?

查看:173
本文介绍了如何绑定ListView值取决于组合框选择的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想绑定我的列表框取决于组合框选择的项目这里是我的代码

Hi all i want to bind my listbox depends on combobox selected item here is my code

       <StackPanel Orientation="Horizontal" >
            <ComboBox Name="cmbID" Width="150"  Margin="10" Height="30" SelectedItem="{Binding CmbSelected,Mode=TwoWay}" DisplayMemberPath="ID" ItemsSource="{Binding MyStudent,Mode=TwoWay}"/>
            <Button Name="btnGetDetail"  Margin="10" Command="{Binding getDetails}" Content="Get Details" Height="30" Width="90"/>
            <TextBox Name="tbName1" Width="90" Height="30" Text="{Binding ElementName=cmbID,Path= SelectedItem.Sub}"></TextBox>
        </StackPanel>

在上面的代码中,我将我的组合框绑定到一个observable集合,并想要将我的ListView绑定到所选项下面的combobox是我的代码

In Above code i am binding my combobox to one observable collection and want to bind my ListView to selected item of combobox below is my code

 <ListView Name="myStudent" ItemsSource="{Binding CmbSelected,UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" Width="420"  Height="150">
            <ListView.View >
                <GridView x:Name="grdStudentDetails">
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.ID}" Width="30"/>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding ElementName=cmbID,Path =SelectedItem.Name}" Width="100"/>
                    <GridViewColumn Header="RollNum" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.RollNum}" Width="100"/>
                    <GridViewColumn Header="Subject" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.Sub}" Width="100"/>
                    <GridViewColumn Header="PhNumber" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.PhNum}" Width="100"/>
                </GridView>


            </ListView.View>
        </ListView>

我无法找到我在哪里犯错误,即使我绑定我的文本框具有相同的绑定工作正常。请参考我的组合框的XAML下方的文本框。

I am not able to find where I am doing mistake even I bind my text box with same binding it is working fine. please refer textbox just below my combobox's XAML.

我的viewmodle.cs代码如下

private student cmbSelcted;

    public student CmbSelected
    {
        get { return cmbSelcted; }
        set { cmbSelcted = value; OnPropertyChanged("CmbSelected"); }
    }


    public ObservableCollection<student> MyStudent
    {
        get { return myStudent; }
        set { myStudent = value; OnPropertyChanged("MyStudent"); }
    }


推荐答案

ItemsSource的ListView!希望你绑定到一个集合。

Can't see the ItemsSource of your ListView! Hope you've bind it to an collection. If not - try this approach.

<ListView Grid.Row="1" ItemsSource="{Binding SelectedStudents, Mode=OneWay}">
            <ListView.View >
                <GridView x:Name="grdStudentDetails">
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="30"/>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"/>
                    <GridViewColumn Header="RollNum" DisplayMemberBinding="{Binding RollNum}" Width="100"/>
                    <GridViewColumn Header="Subject" DisplayMemberBinding="{Binding Sub}" Width="100"/>
                    <GridViewColumn Header="PhNumber" DisplayMemberBinding="{Binding PhNum}" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>

ViewModel

ViewModel

private Student _cmbSelected;
public Student CmbSelected
{
    get { return _cmbSelected; }
    set
    {
        _cmbSelected = value;
        if (_cmbSelected != null)
        {
            SelectedStudents = new List<Student>() { _cmbSelected };
        }
        else
        {
            SelectedStudents = new List<Student>();
        }
        OnPropertyChanged();
    }
}

private List<Student> _selectedStudents;
public List<Student> SelectedStudents
{
    get { return _selectedStudents; }
    set
    {
        _selectedStudents = value;
        OnPropertyChanged();
    }
}

希望你看到一个ItemsSource你的ListView。

Hope you see that it's important to have an ItemsSource on your ListView.

这篇关于如何绑定ListView值取决于组合框选择的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆