[英]WPF MVVM Combobox IsEditable=“True”
I have this issue, I must use IsEditable="True" in a combobox but when I select an item I get in the text "Travel_order.Model", while if i remove IsEditable="True" I get an item. 我有这个问题,我必须在组合框中使用IsEditable =“ True”,但是当我选择一个项目时,我会在文本中输入“ Travel_order.Model”,而如果我删除IsEditable =“ True”,则会得到一个项目。 (see the picture) How can i resolved ?
(见图)我该如何解决?
this is the code. 这是代码。
<ComboBox x:Name="Cmb_Uti"
ItemsSource="{Binding User_Utilizzatore, Mode=TwoWay}"
SelectedValuePath="Value"
SelectedItem="{Binding SelectUser_Utilizzatore, Mode=TwoWay}"
IsEditable="True"
Grid.Row="2" Grid.Column="3"
HorizontalAlignment="Left" Height="23" Margin="5,17,0,0"
VerticalAlignment="Top" Width="176">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DescUtente, UpdateSourceTrigger=PropertyChanged}" Padding="10,0,0,0"></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Effect>
<DropShadowEffect Color="#FF0A0A0A" Opacity="0.6"/>
</ComboBox.Effect>
</ComboBox>
Public Property User_Utilizzatore As ObservableCollection(Of Model_User_Utilizzatore)
Private _SelectUser_Utilizzatore As Model_User_Utilizzatore
Public Property SelectUser_Utilizzatore As Model_User_Utilizzatore
Get
Return _SelectUser_Utilizzatore
End Get
Set(value As Model_User_Utilizzatore)
_SelectUser_Utilizzatore = value
OnPropertyChanged("SelectUser_Utilizzatore")
End Set
End Property
You don't have to define ItemTemplate as textBlock. 您不必将ItemTemplate定义为textBlock。 When you do that and add IsEditable = true, the item will become a textbox.
当您这样做并添加IsEditable = true时,该项目将成为文本框。 To Avoid this, just use DisplayMemberPath:
为了避免这种情况,只需使用DisplayMemberPath:
<ComboBox x:Name="Cmb_Uti"
ItemsSource="{Binding User_Utilizzatore, Mode=TwoWay}"
SelectedValuePath="Value"
SelectedItem="{Binding SelectUser_Utilizzatore, Mode=TwoWay}"
IsEditable="True"
DisplayMemberPath="DescUtente"
Grid.Row="2" Grid.Column="3"
HorizontalAlignment="Left" Height="23" Margin="5,17,0,0"
VerticalAlignment="Top" Width="176">
<ComboBox.Effect>
<DropShadowEffect Color="#FF0A0A0A" Opacity="0.6"/>
</ComboBox.Effect>
</ComboBox>
Found a neat little workaround: You can bind the IsEditable property to a bool and switch it to false when the bound SelectedItem is set. 找到了一个巧妙的解决方法:可以将IsEditable属性绑定到bool,并在设置绑定的SelectedItem时将其切换为false。 Works just as wanted...
可以按需工作...
<ComboBox ItemsSource="{Binding YourSource}"
IsReadOnly="True"
SelectedItem="{Binding YourSelectedItem}"
Text="Please select..."
IsEditable="{Binding ComboBoxEditable}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding ToWhatever}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and the code: 和代码:
private Object _YourSelectedItem;
public Object YourSelectedItem { get { return _YourSelectedItem; } set { YourSelectedItem = value); ComboBoxEditable = false; } }
private Boolean _ComboBoxEditable;
public Boolean ComboBoxEditable { get { return _ComboBoxEditable; } set { _ComboBoxEditable = value); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.