2016-12-01 64 views
0

大家好,我想我的列表框綁定取決於組合框中選擇的項目,這裏是我的代碼如何綁定ListView值取決於組合框選定的項目?

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

我無法找到我在做什麼錯誤,即使我綁定我的文本框與相同的綁定它工作正常。請參閱我的ComboBox的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

什麼是你所得到的錯誤? – Prajwal

+0

有沒有錯誤,但也有約束力現在正在反映 – Sumitk

回答

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是很重要的。

+0

你好先生, 是啊我已經將itemsource綁定到集合,但它仍然沒有反映在binding.i已編輯我的帖子PLZ有看。 還有一件事,如果我應用相同的綁定到文本框它的工作,但在列表視圖它不反映某處我丟失的東西 – Sumitk

+0

雖然我仍然無法看到ListView的ItemsSource綁定。請修改它。 – ManDani

+0

將列表/集合綁定到你的'ListView'' ItemsSource',並且一切正常。像我一樣,也請更改'DisplayMemberBinding'上的綁定路徑。 – ManDani

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

有什麼意見?你爲什麼認爲這會起作用? –

相關問題