![](/img/trans.png)
[英]How depending the selected value of a combobox, i can allow in a numericupdown the value to be >= 0?
[英]How do I allow a ComboBox to have a selected value that is not in ItemsSource?
我有一個這樣的ComboBox:
<ComboBox IsEditable="True"
ItemsSource="{Binding SelectableValues}"
SelectedValue="{Binding SelectedValue}" />
SelectableValues
是double
的列表, SelectedValue
是double
。 如果用戶從列表中選擇一個值或手動輸入這些值之一,則我的SelectedValue
屬性將更新。 但是,如果用戶手動輸入另一個值,則不是。 我如何允許SelectedValue
采用ItemsSource
其他值?
編輯 :認為它像MS Word中的字體大小框。 我可以從列表中選擇一個值,或者提供自己的值。
創建一個繼承comboBox的用戶控件。 將依賴項屬性添加為“ SelectedText”。 在組合框上為LostFocus創建事件處理程序,在事件處理程序中分配輸入值依賴項屬性“ SelectedText”。 如果其值是new,則在其setter中綁定“ SelectedText”,然后將廣告收集到集合中,並將SelectedValue設置為新值。
間接地,您必須通過在ComboBox中添加新屬性來更新源。
public class ExtendedComboBox : ComboBox
{
public ExtendedComboBox()
{
this.IsEditable = true;
this.LostFocus += ComboBox_LostFocus;
}
public string SelectedText
{
get
{
return (string)GetValue(SelectedTextProperty);
}
set
{
SetValue(SelectedTextProperty, value);
}
}
public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(string), typeof(ExtendedComboBox), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnSelectedTextPropertyChanged)));
private static void OnSelectedTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
private void ComboBox_LostFocus(object sender, RoutedEventArgs e)
{
SelectedText = (e.Source as ComboBox).Text??string.Empty;
}
}
// Binding Example
%gt%local:ExtendedComboBox Margin="3" x:Name="ecb" SelectedText="{Binding SelectedText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding SelectedTextList}">%gt/local:ExtendedComboBox>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.