2017-02-14 91 views
-1

綁定的組合框下面是代碼問題與WPF

<ComboBox Name="cmbRegisteredDriveList" 
      Width="150" HorizontalAlignment="Center" 
      ItemsSource="{Binding Path=DriveList}" 
      SelectedItem="{Binding Path=SelectedDrive, Mode=TwoWay}" 
      IsEnabled="{Binding IsBusy, Converter={StaticResource NotConverter}}" 
      ItemContainerStyle="{StaticResource xxxx.ComboBoxItem.Style}"> 

    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock x:Name="tbTemplate" Width="250" Visibility="Collapsed"/> 
       <TextBlock TextWrapping="NoWrap" 
          Text="{Binding VolumeLabel, Converter={StaticResource CenterEllipsisConverter}, ConverterParameter={x:Reference tbTemplate}}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

當過我們更改卷標爲驅動,我們正在通知。但組合框中的選定項目不會刷新。誰可以幫我這個事。我想在組合框中顯示選定項目的更改卷標。

public ObservableCollection<DiskDrive> _driveList; 
public ObservableCollection<DiskDrive> DriveList { get { return _driveList; } } 
private DiskDrive _selectedDrive; 
public DiskDrive SelectedDrive 
{ 
    get { return _selectedDrive; } 
    set { _selectedDrive = value; NotifyPropertyChanged(() => SelectedDrive); } } 

此外,我們會在需要時通知它。

NotifyPropertyChanged(() => DriveList); 
NotifyPropertyChanged(() => SelectedDrive); 

class DiskDrive,物業VolumeLabel的定義是這樣的:

/// <summary> 
/// Get the volume name of this disk. This is the friendly name ("Stick"). 
/// </summary> 
/// <remarks> 
/// When this class is used to identify a removed USB device, the Volume 
/// property is set to String.Empty. 
/// </remarks> 
private string _volumeLabel; 
public string VolumeLabel 
{ 
    get { return _volumeLabel; } 
    set { _volumeLabel = string.IsNullOrWhiteSpace(value) ? string.Format(LocalizationManager.Instance["XXXX"], SerialNumber) : value; } 
} 
+2

你能告訴我們'SelectedDrive'的定義嗎? – wkl

+2

如何更改_VolumeLabel_? – Ron

+0

嘗試綁定中的'UpdateSourceTrigger'。 – AnjumSKhan

回答

0

DiskDrive必須實現改變時INotifyPropertyChangedVolumeLabel必須提高PropertyChanged事件。否則,綁定不會被更新。

此外,請注意,當綁定到不執行INotifyPropertyChanged的類的屬性時,您將最有可能泄漏內存。請參閱here