0

我有一个组合框。每个项目由一个标题和一个描述组成。带有标题和描述的项目

我想在第二项中使用一个文本框。这很好用。;) 在此处输入图像描述

现在我想问(因为带有文本框的项目高于所有其他项目),是否有可能如果选择的项目(带有文本框)不显示文本框,只显示内容作为字符串?

在此处输入图像描述

<ComboBox Height="35" Margin="0 2 0 2" SelectedIndex="0">
    <ComboBoxItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Source="/WpfApplication14;component/Resources/Icon1.ico" Height="22" Width="22" Grid.Column="0" HorizontalAlignment="Left" />
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                    <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                </Grid.RowDefinitions>
                <TextBlock Text="Item Titel 1" Grid.Row="0" FontWeight="Bold" />
                <TextBlock Text="Item Beschreibung 1" Grid.Row="1" FontStyle="Italic">
                    <TextBlock.TextEffects><TextEffect Foreground="#FF555454" /></TextBlock.TextEffects>
                </TextBlock>
            </Grid>
        </Grid>
    </ComboBoxItem>
    <ComboBoxItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Source="/WpfApplication14;component/Resources/Icon2.ico" Height="22" Width="22" Grid.Column="0" HorizontalAlignment="Left" />
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                    <RowDefinition Height="24" MinHeight="24" MaxHeight="24" />
                </Grid.RowDefinitions>
                <TextBlock Text="Item Titel 2" Grid.Row="0" FontWeight="Bold" />
                <TextBox Grid.Row="1">
                    <TextBox.Text>c:\temp\test</TextBox.Text>
                    <TextBox.Style>
                        <Style>
                            <Setter Property="TextBox.Height" Value="20"/>
                        </Style>
                    </TextBox.Style>
                </TextBox>
            </Grid>
        </Grid>
    </ComboBoxItem>
</ComboBox>
4

2 回答 2

1

您可以通过使用 TextBox 来编辑文本(就像您所做的那样)和 TextBlock 在选择项目时显示文本来实现这一点。

您可以通过将它们的 Visibility 绑定到 ComboBoxItem 的 IsSelected 值来显示/隐藏 TextBlock/TextBox,并使用 ValueConverter 将 true/false 值转换为 Visible/Collapsed。

我在这里对您的代码进行了一些编辑,以使其更简单:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox Name="myComboBox" Margin="0 2 0 2" SelectedIndex="0" Grid.Row="1">
        <ComboBoxItem>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                        <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Item Titel 1" Grid.Row="0" FontWeight="Bold" />
                    <TextBlock Text="Item Beschreibung 1" Grid.Row="1" FontStyle="Italic" Foreground="#FF555454" />
                </Grid>
            </Grid>
        </ComboBoxItem>
        <ComboBoxItem Name="myComboBoxItem">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Item Titel 2" Grid.Row="0" FontWeight="Bold" />

                    <TextBox Grid.Row="1" Name="myTextBox" Text="c:\temp\test" Height="20" Visibility="{Binding ElementName=myComboBoxItem, Path=IsSelected, Converter={ValueConverter:BooleanToVisibilityConverter}}" />
                    <TextBlock Name="myTextBlock" Text="{Binding ElementName=myTextBox, Path=Text}" Grid.Row="1" FontStyle="Italic" Foreground="#FF555454" Visibility="{Binding ElementName=myComboBoxItem, Path=IsSelected, Converter={ValueConverter:BooleanToVisibilityConverter}, ConverterParameter=Inverted}" />
                </Grid>
            </Grid>
        </ComboBoxItem>
    </ComboBox>
</Grid>

ValueConverter 的代码:

public abstract class BaseConverter : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

public class BooleanToVisibilityConverter : BaseConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (parameter != null && parameter.ToString().Equals("Inverted"))
        {
            if ((bool)value)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }
        if ((bool)value)
            return Visibility.Collapsed;
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if (parameter.ToString().Equals("Inverted"))
                return (Visibility)value != Visibility.Visible;
            return (Visibility)value == Visibility.Visible;
        }
        catch (Exception e)
        {
            // Error handling
            return false;
        }
    }
}
于 2012-07-10T09:15:53.857 回答
1

代替:

        <TextBox Grid.Row="1">
            <TextBox.Text>c:\temp\test</TextBox.Text>
            <TextBox.Style>
                <Style>
                    <Setter Property="TextBox.Height" Value="20"/>
                </Style>
            </TextBox.Style>
        </TextBox>

和:

        <TextBox Grid.Row="1" Text="c:\temp\test" Height="20" BorderThickness="0" Padding="0" />

但你仍然会有一个问题:

你的组合框是 35 像素高:

<ComboBox Height="35" Margin="0 2 0 2" SelectedIndex="0">

但你的 ComboBoxItem 是 16 + 24 = 40 像素高:

            <Grid.RowDefinitions>
                <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                <RowDefinition Height="24" MinHeight="24" MaxHeight="24" />
            </Grid.RowDefinitions>

所以无论如何它都会被截断。要么让你的 comboBox 更大,要么让你的 comboBoxItem 更小。

旁注:您不需要对文本块的前景使用文本效果:

    <TextBlock Foreground="#FF555454" />

也可以

于 2012-07-10T08:16:05.137 回答