简体   繁体   English

WPF MVVM组合框IsEditable =“ True”

[英]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.

相关问题 WPF IsEditable = true填充对象的ComboBox将ToString()显示为所选项 - WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item 带有IsEditable =“True”的WPF ComboBox - 如何指示未找到匹配项? - WPF ComboBox with IsEditable=“True” - How can I indicate that no match was found? WPF组合框可编辑,不可编辑 - WPF combobox iseditable not making editable ComboBox IsEditable Behavior问题WPF - ComboBox IsEditable Behavior question WPF 如果IsEditable和IsReadOnly为true,如何使WPF ComboBox键盘文本搜索正常工作? - How to make that WPF ComboBox keyboard text search will works correctly if IsEditable and IsReadOnly is true? 如果我从一个 IsEditable="True" 的 ComboBox 切换到另一个,则 WPF 中 DataGrid 的 SelectedItem 不会触发 - The SelectedItem of DataGrid in WPF does not fire if I switch from one ComboBox with IsEditable="True" to another IsEditable,ItemsSource和ValidationRule的WPF ComboBox交互 - WPF ComboBox interaction of IsEditable, ItemsSource, and ValidationRule WPF Combobox isEditable 绑定条目到列表 - WPF Combobox isEditable bind entry to list WPF ComboBox-在不同线程中绑定到IsEditable不一致 - WPF ComboBox - binding to IsEditable in different thread is not consistent ComboBox中的WPF IsEditable - 如何删除自动完成 - WPF IsEditable in ComboBox - how to remove autocomplete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM