大家好我想绑定我的列表框取决于组合框所选项目这里是我的代码
<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下面的文本框。
我的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"); }
}
答案 0 :(得分:0)
看不到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很重要。
答案 1 :(得分:0)
<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>