WPF MVVM组合框IsEditable =" True"

时间:2014-12-24 11:40:37

标签: wpf

我有这个问题,我必须在组合框中使用IsEditable =“True”,但当我选择一个项目时,我会在文本“Travel_order.Model”中找到,而如果我删除IsEditable =“True”,我会得到一个项目。 (见图) 我怎么解决?

这是代码。

            <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

2 个答案:

答案 0 :(得分:0)

您不必将ItemTemplate定义为textBlock。当您这样做并添加IsEditable = true时,该项将成为文本框。 要避免这种情况,只需使用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>

答案 1 :(得分:0)

找到一个简洁的小解决方法:您可以将IsEditable属性绑定到bool,并在设置绑定的SelectedItem时将其切换为false。工作正常...

ngFor

和代码:

     <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>