ComboBox斗争

时间:2014-11-07 07:29:50

标签: c# wpf combobox

我试图在visual studio中创建一个组合框。它非常习惯,我在显示项目时遇到了很大的问题。

默认情况下,项目仅显示绑定名称 当鼠标结束时,会出现两个按钮,一个是编辑项目,另一个是删除它 - 该部分工作正常 问题是当项目被选中时。我想在ComboBox内容演示者中隐藏这些按钮,但希望将它们保留在弹出窗口中。

最简单的解决方案是在弹出选中时弹出项目,但如何?其他想法?

<ComboBox.ItemTemplate>
    <DataTemplate DataType="{x:Type controls:TEMP}">
        <Grid Height="42" Width="{StaticResource ToggleButtonWidth}">

            <!--Selected Item Display-->
            <Grid Name="DefaultView">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="25"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="25"/>
                </Grid.ColumnDefinitions>

                <TextBox x:Name ="NameDisplay"
                         Text="{Binding Name, Mode=TwoWay}"
                         Grid.Column="1"
                         HorizontalAlignment="Left"
                         VerticalContentAlignment="Center"
                         IsEnabled="{Binding Path=IsChecked, ElementName=EditButton}">
                    <TextBox.Style>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="OverridesDefaultStyle" Value="True"/>
                            <Setter Property="VerticalContentAlignment" Value="Center"/>
                            <Setter Property="HorizontalContentAlignment" Value="Center"/>
                            <Setter Property="BorderThickness" Value="0"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type TextBox}">
                                        <ScrollViewer Margin="0"
                                                        x:Name="PART_ContentHost" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Selected}" Value="False">
                                    <Setter Property="HorizontalContentAlignment" Value="Left"/>
                                </DataTrigger>
                                <Trigger Property="IsEnabled" Value="True">
                                    <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=NameDisplay}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <Button x:Name="DeleteButton"  
                        Grid.Column="0" 
                        Width="20" Height="20" 
                        Visibility="{Binding MouseOver, Converter={StaticResource BoolToVis}}">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <EventSetter Event="Click" Handler="DeleteItem"/>
                            <Setter Property="Margin" Value="0,0"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <ContentPresenter/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Button.Style>
                    <Image Source="Images/button_cancel.png"
                           Width="20" Height="20" />
                </Button>
                <ToggleButton x:Name="EditButton"
                        Grid.Column="2" 
                        Width="20" Height="20" 
                        Visibility="{Binding MouseOver, Converter={StaticResource BoolToVis}}"
                        Content="e">
                </ToggleButton>
            </Grid>
        </Grid>
    </DataTemplate>
</ComboBox.ItemTemplate>

1 个答案:

答案 0 :(得分:0)