![](/img/trans.png)
[英]WPF ComboBox: Set SelectedItem to item not in ItemsSource -> Binding oddity
[英]Set the SelectionChanged event of a ComboBox while binding its SelectedItem and ItemsSource in XAML
我正在嘗試建立一個ComboBox,其選項從字符串列表中綁定,其默認選擇值從設置中綁定,並更改其選擇的事件處理程序。
我想像這樣使用XAML來配置它們:
<ComboBox Name="RoutesComboBox"
ItemsSource="{Binding Routes}"
SelectedItem="{Binding DefaultRoute}"
SelectionChanged="RouteFilter_SelectionChanged" />
但是當我在啟動時這樣做會引發錯誤:
PresentationFramework.dll中發生了類型為'System.Reflection.TargetInvocationException'的未處理異常
如果僅在XAML中進行某些操作,則可以在C#中以編程方式設置SelectionChanged
事件或ItemsSource
,如下所示,它可以正常工作。 但是我有很多這樣的ComboBoxes,所以我寧願直接在XAML中進行操作。
<ComboBox Name="RoutesComboBox"
ItemsSource="{Binding Routes}"
SelectedItem="{Binding DefaultRoute}" />
使用此C#:
public IEnumerable<string> Routes
{
get { return LubricationDatabase.GetRoutes(); }
}
public string DefaultRoute
{
get { return MySettings.Default.DefaultRoute; }
set { } /* side question: without this, it throws a parse exception. Any idea why? */
}
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
RoutesComboBox.SelectionChanged += RouteFilter_SelectionChanged;
}
我也嘗試過在這里找到的解決方案:
private string _defaultRoute;
public string DefaultRoute
{
get { return MySettings.Default.DefaultRoute; }
set
{
if (_defaultRoute != value)
{
_defaultRoute = value;
// this fires before `SelectedValue` has been
// updated, and the handler function uses that,
// so I manually set it here.
RoutesComboBox.SelectedValue = value;
SelectionChangedHandler();
}
}
}
沒關系,但是當我可以以編程方式分配SelectionChanged
事件時,它卻很笨重,並且可能需要做的工作更多。
再次,如果可能的話,我想使用XAML來完成所有操作,因為我有很多這樣的ComboBox,並且像這樣在C#中將它們初始化都非常糟糕。
有任何想法嗎?
當用戶更改選擇時不更新項目時,為什么要綁定到SelectedItem
? 不知道事件處理程序在做什么,但是我有一個可以按您想要的方式工作的解決方案。
簡而言之,您需要使用后備字段來跟蹤DefaultRoute
。 另外,當選定的項在視圖模型中更改時,您需要通知UI; 順便說一句,您似乎並沒有在做MVVM。 如果您打算以某種方式更新視圖,則應該僅掛接到選擇更改事件。 所有其他更改應在視圖模型DefaultRoute
setter中處理
<ComboBox Name="RoutesComboBox"
ItemsSource="{Binding Routes}"
SelectedItem="{Binding DefaultRoute}"
SelectionChanged="RouteFilter_SelectionChanged" />
public partial class MainWindow : Window, INotifyPropertyChanged
{
public IEnumerable<string> Routes
{
get
{
return new string[] { "a", "b", "c", "d" };
}
}
public string DefaultRoute
{
get
{
return _defaultRoute;
}
set
{
_defaultRoute = value;
// Handle saving/storing setting here, when selection has changed
//MySettings.Default.DefaultRoute = value;
NotifyPropertyChanged();
}
}
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
DefaultRoute = MySettings.Default.DefaultRoute;
}
private string _defaultRoute;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void RouteFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
public static class MySettings
{
public static class Default
{
public static string DefaultRoute = "a";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.