【问题标题】:How To bind ListView values depends on combobox selected item?如何绑定 ListView 值取决于组合框选定项?
【发布时间】:2016-12-01 07:11:05
【问题描述】:

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

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

在上面的代码中,我将我的组合框绑定到一个可观察的集合,并希望将我的 ListView 绑定到下面的组合框的选定项是我的代码

 <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 下方的文本框。

我的 viewmodel.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"); }
    }

【问题讨论】:

  • 您遇到了什么错误?
  • 没有错误,但现在正在反映绑定

标签: c# wpf listview combobox


【解决方案1】:

看不到 ListView 的 ItemsSource!希望您已将其绑定到集合。如果没有,请尝试这种方法。

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

视图模型

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();
    }
}

希望您看到在您的 ListView 上有一个 ItemsSource 很重要。

【讨论】:

  • 你好先生,是的,我已经将 itemsource 绑定到一个集合,但它仍然没有反映在绑定中。我已经编辑了我的帖子,请看一下。如果我将相同的绑定应用到文本框它的工作但在列表视图中它没有反映某处我错过了一些东西,还有一件事
  • 虽然我仍然看不到您的 ListView 的 ItemsSource-Binding。请也编辑它。
  • 将列表/集合绑定到您的ListView ItemsSource,一切正常。请像我一样更改“DisplayMemberBinding”上的绑定路径。
  • 我试过了,但仍然没有任何反应。 drive.google.com/open?id=0B4fArMytRDVPREF3bDZBT0tudEk 请查看我的应用程序。请查看右侧组合框和列表视图下方
【解决方案2】:

    <Grid>
        <StackPanel>
            <ListView x:Name="usergrid" Margin="100,50,100,0" HorizontalAlignment="Center" FontSize="20" ItemsSource="{Binding Path=user1}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="UserId"       DisplayMemberBinding="{Binding UserId,Mode=TwoWay}" Width="100" ></GridViewColumn>
                        <GridViewColumn Header="First Name"   DisplayMemberBinding="{Binding FirstName,Mode=TwoWay}"  Width="150" />
                        <GridViewColumn Header="Last Name"    DisplayMemberBinding="{Binding LastName,Mode=TwoWay}" Width="150" />
                        <GridViewColumn Header="City"         DisplayMemberBinding="{Binding City,Mode=TwoWay}" Width="150" />
                        <GridViewColumn Header="State"        DisplayMemberBinding="{Binding State,Mode=TwoWay}" Width="150" />
                        <GridViewColumn Header="Country"      DisplayMemberBinding="{Binding Country,Mode=TwoWay}" Width="150" />
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 2019-12-27
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多