2015-10-20 40 views
1

我在WPF中的數據綁定中苦苦尋找過我仍然看不到我缺少的模型以更新所選項目名單。 我有2個實體插槽和SlotTypeWPF twoway在WPF中建模的數據綁定列表框無法獲取選定的值

public class Slot : BindableObject, INotifyPropertyChanged 
{ 
    private int slotID; 
    public int SlotID 
    { 
     get { return slotID; } 
     set 
     { 
      if (slotID != value) 
      { 
       slotID = value; 
       RaisePropertyChanged("SlotID"); 
      } 
     } 
    } 
    private string user; 
    public string SlotUser 
    { 
     get { return user; } 
     set 
     { 
      if (user != value) 
      { 
       user = value; 
       RaisePropertyChanged("SlotUser"); 
      } 
     } 
    } 

    private int slotUserID; 
    public int SlotUserID 
    { 
     get { return slotUserID; } 
     set 
     { 
      if (slotUserID != value) 
      { 
       slotUserID = value; 
       RaisePropertyChanged("SlotUserID"); 
      } 
     } 
    } 

    private int slotType; 
    public int SlotType 
    { 
     get { return slotType; } 
     set 
     { 
      if (slotType != value) 
      { 
       slotType = value; 
       RaisePropertyChanged("SlotType"); 
      } 
     } 
    } 

    private DateTime slotStartDateTime; 
    public DateTime SlotStartDateTime 
    { 
     get { return slotStartDateTime; } 
     set 
     { 
      if (slotStartDateTime != value) 
      { 
       slotStartDateTime = value; 
       RaisePropertyChanged("SlotStartDateTime"); 
      } 
     } 
    } 
public class SlotType : BindableObject, INotifyPropertyChanged 
{ 
    private int slotTypeID; 
    public int SlotTypeID 
    { 
     get { return slotTypeID; } 
     set 
     { 
      if (slotTypeID != value) 
      { 
       slotTypeID = value; 
       RaisePropertyChanged("SlotTypeID"); 
      } 
     } 
    } 
    private string slotTypeDesc; 
    public string SlotTypeDesc 
    { 
     get { return slotTypeDesc; } 
     set 
     { 
      if (slotTypeDesc != value) 
      { 
       slotTypeDesc = value; 
       RaisePropertyChanged("SlotTypeDesc"); 
      } 
     } 
    } 
    public override string ToString() 
    { 
     return SlotTypeDesc; 
    } 
} 

我然後創建一個包含一個類,並設置我的窗口背景是這個類。

public class SlotWithTypes 
{ 
    public ObservableCollection<SlotType> slotTypes { get; set; } 
    public Slot slot { get; set; } 
} 

從我的主窗口,我測試,如果需要在新窗口打開顯示SlotTypes列表供用戶選擇哪種類型有關創建插槽。

Slot slot = new Slot(); 
slot.SlotStartDateTime = item.SlotStartDateTime; 
if (item.SlotType == 0) 
{ 
    SlotWithTypes swtype = new SlotWithTypes(); 
    swtype.slotTypes = slotTypes; 
    swtype.slot = slot; 

    SelectSlotType stype = new SelectSlotType(); 
    stype.DataContext = swtype; 
    stype.ShowDialog(); 
} 

最後,在我的XAML我有我的列表框

<Grid> 
    <ListBox Name="lstSlotTypes" 
      HorizontalAlignment="Left" 
      Height="200" 
      Margin="0,10,0,0" 
      VerticalAlignment="Top" 
      Width="194"     
      ItemsSource="{Binding slotTypes}" 
      SelectedItem="{Binding Path=slot.Type, Mode=TwoWay}" 
      SelectedValue="{Binding slotTypes.SlotTypeID, Mode=TwoWay}" 
      SelectedValuePath="{Binding slot.Type, Mode=TwoWay}" 
      DisplayMemberPath="{Binding slotTypes.SlotTypeDesc}"     
      SelectionChanged="lstSlotTypes_SelectionChanged"> 
    </ListBox> 
    <TextBox HorizontalAlignment="Left" 
      Height="23" 
      Margin="10,246,0,0" 
      TextWrapping="Wrap" 
      Text="{Binding slot.SlotStartDateTime, Mode=TwoWay}" 
      VerticalAlignment="Top" 
      Width="74"/> 
</Grid> 

爲了測試我把綁定到哪個工作和更新回到我的模型slotstartdatetime一個文本框。我已經嘗試了列表框上的各種綁定格式,並且可以獲取我的SlotTypes列表來顯示put無法使用所選值更新Slot實體。

我意識到這已經成爲一個長期的問題,但請如果有人能看到我做錯了什麼?

+0

這是什麼邏輯? 1.我打開窗戶時有一系列物品? 2.列表框中是否有任何項目? – Ilan

+0

您確定,在SelectedValuePath和DisplayMemberPath中您需要綁定? :)必須有一些屬性名稱。 – Spawn

回答

1

我可以看到很多事情錯在這裏,首先,這是怎樣的XAML看起來應該得到這個工作:

<ListBox Name="lstSlotTypes" HorizontalAlignment="Left" Height="200" Margin="0,10,0,0" VerticalAlignment="Top" Width="194"     
     ItemsSource="{Binding slotTypes}" 
      SelectedValue="{Binding Path=slot.SlotType}" 
      SelectedValuePath="SlotTypeID" 
    DisplayMemberPath="SlotTypeDesc" > 
    </ListBox> 

現在,當你不知道什麼是綁定不工作,在大多數這些情況是因爲輸出窗口中可見的綁定錯誤,所以它可能非常有幫助,因此每次都在這裏查看。例如,在您的xaml上,這是輸出窗口顯示的內容:

System.Windows.Data Error: 40 : BindingExpression path error: 'Type' property not found on 'object' ''Slot' (HashCode=62085918)'. BindingExpression:Path=slot.Type; DataItem='SlotWithTypes' (HashCode=23184054); target element is 'ListBox' (Name='lstSlotTypes'); target property is 'SelectedItem' (type 'Object') System.Windows.Data Error: 40 : BindingExpression path error: 'SlotTypeID' property not found on 'object' ''ObservableCollection`1' (HashCode=58050239)'. BindingExpression:Path=slotTypes.SlotTypeID; DataItem='SlotWithTypes' (HashCode=23184054); target element is 'ListBox' (Name='lstSlotTypes'); target property is 'SelectedValue' (type 'Object')

如您所見,已經有一些信息。

現在有什麼不對您的XAML:

  • Slot類沒有一個Type財產,只有SlotType財產
  • 不能綁定到slot.SlotTypeSelectedItem因爲SelectedItem持有來自ItemsSource的整個選定對象 - 在這種情況下爲SlotType類,而綁定屬性slot.SlotType的類型爲int。所以,你需要使用SelectedValue屬性,而不是SelectedItem在這種情況下
  • 它看起來像你SelectedValuePathSelectedValue結合點,應該在哪裏存儲所選項目的值的DataContext對象搞砸SelectedValue,在這種情況下, = slot.SlotType
  • SelectedValuePath - 它應在其上設置爲所選值所選擇的項目的路徑 - 這是一個靜態字符串,無需結合任何
  • 的DisplayMemberPath - 如上述,它是指示該路徑的靜態字符串持有需要顯示的值的財產 - 無需在此綁定任何東西
+0

非常感謝Alek,這是一個很好的答案,以及如此多的信息。 –

+0

@ user5466211只是爲了好玩,看看這篇[文章](http://www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele)。它會幫助你一點我希望。 – Noctis

+1

@Noctis感謝非常有幫助,我一直在尋找年齡,並找不到像阿列克的答案和您的文章有幫助。 –

1

有AR一個幾個問題的代碼:

  1. 有(當列表框源被定義,幷包含元件的情況下)在Visual Studio輸出窗口兩個綁定錯誤,其原因是錯的結合SelectedValuePath的源類型衝突。這是爲我工作的解決方案。
  2. 數據上下文集合初始化:

    private void InitDataContext() 
    { 
        DataContext = new SlotWithTypes 
        { 
         slot = new Slot(), 
         slotTypes = new ObservableCollection<SlotType>(new List<SlotType> 
         { 
          new SlotType 
          { 
           SlotTypeDesc = "Bla Bla 1", 
           SlotTypeID = 1, 
          }, 
          new SlotType 
          { 
           SlotTypeDesc = "Bla Bla 2", 
           SlotTypeID = 2, 
          } 
         }) 
        }; 
    } 
    
  3. XAML代碼:

    <Grid> 
    <ListBox Name="lstSlotTypes" HorizontalAlignment="Left" 
         Height="200" Margin="0,10,0,0" 
         VerticalAlignment="Top" Width="194" 
         ItemsSource="{Binding slotTypes}" 
         SelectedValue="{Binding SelectedSlotType, Mode=TwoWay}" 
         SelectedValuePath="SlotTypeID" DisplayMemberPath="SlotTypeDesc" 
         SelectionChanged="lstSlotTypes_SelectionChanged"> 
    </ListBox></Grid> 
    
  4. SlotWithTypes代碼:

    public ObservableCollection<SlotType> slotTypes { get; set; } 
    
    public Slot slot 
    { 
        get { return _slot; } 
        set 
        { 
         _slot = value; 
         OnPropertyChanged(); 
        } 
    } 
    
    public int SelectedSlotType 
    { 
        get { return _selectedSlotType; } 
        set 
        { 
         _selectedSlotType = value; 
         OnPropertyChanged(); 
         UpdateSlot(SelectedSlotType); 
        } 
    } 
    
    private void UpdateSlot(int selectedSlotType) 
    { 
        slot.SlotType = selectedSlotType; 
    } 
    
  5. 結果每次我做出選擇SlotType屬性的廣告位將會隨新值一起更改廣告。在這裏做你的邏輯。

問候,

+0

@Golik感謝你的努力,你說得對我的xaml是錯的 –

+0

@ user5466211不客氣。 – Ilan