【问题标题】:Show shortened file path in ComboBox, but full path in ComboBox dropdown在 ComboBox 中显示缩短的文件路径,但在 ComboBox 下拉列表中显示完整路径
【发布时间】:2018-04-06 04:33:00
【问题描述】:

我试图弄清楚如何在 ComboBox 选择中显示与 ComboBox 下拉列表中显示的字符串不同的字符串。

<ComboBox SelectedItem="{Binding LocalFolderSelection, Mode=OneWayToSource}"
          ItemsSource="{Binding LocalFolders}"
          SelectedIndex="0"/>

那是我的组合框。 LocalFolders 包含一个基本上是文件路径的字符串列表。由于有限的 UI 空间,我不能使 ComboBox 非常宽,以便它可以显示整个字符串。下拉菜单会自动缩放以适应整个路径,这很好,但我需要将显示的选择减少到文件名。

有什么想法可以实现吗? 我希望有一些属性可以用来定义显示文本,并可能使用一个转换器将其绑定到 SelectedItem 上,该转换器会切断路径,但到目前为止我还没有找到类似的东西。

【问题讨论】:

标签: c# wpf xaml combobox


【解决方案1】:

我相信我有你正在寻找的工作。使用带有自定义 ItemTemplate 的转换器,我们可以显示缩短的路径,并且 ContentPresenter 已被修改以保留完整路径。

XAML:

<Window.Resources>
    <converters:ShortenFilePathConverter x:Key="ShortenFilePathConverter" />
</Window.Resources>

...

<ComboBox SelectedItem="{Binding LocalFolderSelection}" 
        ItemsSource="{Binding LocalFolders}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Converter={StaticResource ShortenFilePathConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border x:Name="Bd"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}">
                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                    <ContentPresenter.Content>
                                        <Label Content="{Binding}"/>
                                    </ContentPresenter.Content>
                                </ContentPresenter>
                            </StackPanel>
                        </Border>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

转换器:

public sealed class ShortenFilePathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        var s = value as string;

        if (s == null)
            return null;

        return Path.GetFileName(s);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多