2011-03-20 7 views
1

SelectedItemおよびItemSourceプロパティがviewmodelにバインドされたコンボボックスがあります。 ItemSource内のオブジェクトは、オブジェクトが期限切れになるという点で時間の影響を受けます。コレクション内のアクティブなアイテムのみをItemSourceに設定できます。特定の時点で、selectedItemオブジェクトがItemSourceに存在しない場合があります。 ItemSourceの通常の動作は、ItemSourceコレクションからオブジェクトを選択することのみを許可することだと思いますが、この場合、ItemSource内に存在しなくなったオブジェクトを選択します。 この動作を実装するにはどうすればよいですか?私は私の問題をサポートするためにここにいくつかのコードを掲載します。ComboBox SelectedItemをItemSourceに存在しないdataObjectに設定します

Window.xaml

<Window x:Class="ExpiredSelectedItem.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <ComboBox Height="23" 
       HorizontalAlignment="Left" 
       Margin="184,68,0,0" 
       Name="comboBox1" 
       VerticalAlignment="Top" 
       Width="120" 
       ItemsSource="{Binding CustomList}" 
       SelectedItem="{Binding ActiveItem}" 
       SelectedValue="Code" 
       SelectedValuePath="{Binding ActiveItem.Code}" 
       DisplayMemberPath="Code" 
       /> 
</Grid> 

Window.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace ExpiredSelectedItem 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     CustomList = new List<DomainObject>(); 

     CustomList.AddRange(new DomainObject[] { 
      new DomainObject() { Code = "A", IsActive =true}, 
      new DomainObject() { Code ="B", IsActive = true}, 
      new DomainObject() { Code = "C", IsActive =true}, 
     }); 

     ActiveItem = new DomainObject() { Code = "D", IsActive = false }; 

     this.DataContext = this; 
    } 


    public List<DomainObject> CustomList { get; set; } 

    public DomainObject ActiveItem { get; set; } 
} 

public class DomainObject 
{ 
    public string Code { get; set; } 
    public bool IsActive { get; set; } 

} 

}

IをコードビハインドのコードDを選択したとしても、場合コンボボックスが最初の項目(A)をロードします。期待された動作は、 "D"がテキストボックスで選択されていたはずだが、ドロップダウンが開かれているときには項目が存在しないはずであるということでした。

答えて

0

これは役割があるのか​​どうかわかりませんが、疑わしいと思われます。それを削除してください:

<ComboBox ... SelectedValue="Code" ... 
+0

おかげ - SelectedValueのとselectedvaluePath異なる目的のためにされており、中にノイズだけされていますこの例。しかし、それは問題を解決しません。最初の項目が選択される代わりに、選択されたテキストが空白になります。上記のように、「D」を選択します。 – Arjun

0

それは次の操作を実行することができますリストにないときに選択した項目を保つために:

  • NULLに設定取得からActiveItemを防ぎます。以下を参照してください。if(value!= null)
  • ComboBoxをクリアします。 comboBox1.SelectedIndex = -1;を参照してください。
  • (あなたはおそらくすでにん)INotifyPropertyChangedの実装を

これらの変更は、良いよりもあなたのコードより多くの害を引き起こすかもしれないが、うまくいけば、これはあなたが開始されます。ここで

は、コードビハインドである:ここで

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Windows; 

namespace ComboBoxSelectedItem 
{ 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
    public event PropertyChangedEventHandler PropertyChanged; 
    void Notify(string propName) 
    { 
     if (PropertyChanged != null) 
     { 
     PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     CustomList = new List<DomainObject>(); 

     CustomList.AddRange(new DomainObject[] { 
     new DomainObject() { Code = "A", IsActive =true}, 
     new DomainObject() { Code ="B", IsActive = true}, 
     new DomainObject() { Code = "C", IsActive =true}, 
     }); 

     this.DataContext = this; 
    } 

    public List<DomainObject> CustomList { get; set; } 

    private DomainObject _activeItem = new DomainObject() { Code = "D", IsActive = false }; 
    public DomainObject ActiveItem 
    { 
     get { return _activeItem; } 
     set 
     { 
     if (value != null) 
     { 
      _activeItem = value; 
     } 

     Notify("ActiveItem"); 
     } 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     ActiveItem = new DomainObject() { Code = "D", IsActive = false }; 
     comboBox1.SelectedIndex = -1; 
    } 
    } 
} 

はXAMLです:ジョンのことを指摘して

<Window x:Class="ComboBoxSelectedItem.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <ComboBox 
     Grid.Row="0" 
     Height="23" 
     HorizontalAlignment="Left" 
     Name="comboBox1" 
     VerticalAlignment="Top" 
     Width="120" 
     ItemsSource="{Binding CustomList}" 
     SelectedItem="{Binding ActiveItem}" 
     DisplayMemberPath="Code"/> 
    <TextBox 
     Grid.Row="1" 
     Text="{Binding Path=ActiveItem.Code, Mode=TwoWay}"/> 
    <Button 
     Grid.Row="2" 
     Content="D" 
     Click="Button_Click"/> 
    </Grid> 
</Window> 
関連する問題