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"がテキストボックスで選択されていたはずだが、ドロップダウンが開かれているときには項目が存在しないはずであるということでした。
おかげ - SelectedValueのとselectedvaluePath異なる目的のためにされており、中にノイズだけされていますこの例。しかし、それは問題を解決しません。最初の項目が選択される代わりに、選択されたテキストが空白になります。上記のように、「D」を選択します。 – Arjun