WPF 组合框中所选项目的模板与复合集合中的项目不同

贝尔特

我有一个复合集合,其中包括:

  • 包含“选择供应商”内容的组合框项目
  • 具有 Vendor 对象集合的集合容器,绑定到组合框 cmbVendor

当从组合框中选择供应商时,将ToString()调用方法。但是,我想显示Name所选供应商对象的属性值在此处输入图片说明 在此处输入图片说明

设置组合框属性DisplayMemberPath='Name'有效,但加载时不再显示“选择供应商”,这是不希望的。

笔记:

  • 有时从代码隐藏中选择供应商对象
  • ToString()由于其他原因,我无法覆盖Vendor 对象的方法

有什么建议么?

XAML

<UserControl.Resources>
    <converters:VendorConverter x:Key='VendorConverter' />
    <CollectionViewSource x:Key='VendorsCollection'
                          Source='{Binding Vendors}'>
    </CollectionViewSource>
  </UserControl.Resources>
  <Grid>
    <ComboBox Name='cmbVendor'
              SelectedItem='{Binding Vendor, Converter={StaticResource VendorConverter}, Mode=TwoWay}'
              IsSynchronizedWithCurrentItem='True'
              IsEditable='True'
              Width='{DynamicResource VendorCmbWidth}'>
      <!--Make sure "Select a vendor" is selected-->
      <i:Interaction.Behaviors>
        <behaviour:SelectFirstItemBehavior />
      </i:Interaction.Behaviors>
      <ComboBox.Resources>
        <DataTemplate DataType='{x:Type objects:Vendor}'>
          <StackPanel Orientation='Horizontal'>
            <TextBlock Text='{Binding Name}' />
          </StackPanel>
        </DataTemplate>
        <DataTemplate DataType='{x:Type system:String}'>
          <StackPanel Orientation='Horizontal'>
            <TextBlock Text='{Binding }' />
          </StackPanel>
        </DataTemplate>
      </ComboBox.Resources>
      <ComboBox.ItemsSource>
        <CompositeCollection>
          <ComboBoxItem Content='Select a vendor' />
          <CollectionContainer Collection='{Binding Source={StaticResource VendorsCollection}}' />
        </CompositeCollection>
      </ComboBox.ItemsSource>

    </ComboBox>
  </Grid>
</UserControl>

供应商转换器

internal class VendorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var vendor = value as Vendor;
            if (vendor != null)
            {
                return vendor;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var comboboxItem = value as ComboBoxItem;
            if (comboboxItem != null)
            {
                return null;
            }

            var vendor = value as Vendor;
            if (vendor != null)
            {
                return vendor;
            }

            return null;
        }
    }

行为

internal class SelectFirstItemBehavior : Behavior<ComboBox>
    {
        protected override void OnAttached()
        {
            AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
            base.OnDetaching();
        }

        private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var combobox = sender as ComboBox;
            if (combobox != null && combobox.SelectedIndex == -1)
            {
                combobox.SelectedIndex = 0;
            }
        }
    }
希腊语40

方法一

您应该能够TextSearch.TextPath="Name"为您的普通物品组合并TextSearch.Text="Select a vendor"直接分配给您的特殊物品:

<ComboBox
    IsEditable="True"
    TextSearch.TextPath="Name">
...

<CompositeCollection>
    <ComboBoxItem Content='Select a vendor' TextSearch.Text="Select a vendor" />
    <CollectionContainer Collection='{Binding Source={StaticResource VendorsCollection}}' />
</CompositeCollection>

方法二

仅在未选择任何内容时显示视觉提示文本:

<ComboBox
    ItemsSource="{Binding Source={StaticResource VendorsCollection}}"
    IsEditable="True">
    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Style.Resources>
                <VisualBrush x:Key="hintText" x:Shared="False"  AlignmentX="Left" Stretch="None">
                    <VisualBrush.Visual>
                        <Grid Background="White">
                            <TextBlock Margin="4 3" Text="Select a vendor"/>
                        </Grid>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="Text" Value="">
                    <Setter Property="Background" Value="{StaticResource hintText}"/>
                </Trigger>
                <Trigger Property="Text" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource hintText}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

通过这种方式,您可以保持您的项目集合中没有额外条目和TextSearch.TextPath实际项目的使用和类似属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

wpf / mvvm-如何制作一个组合框,以设置mvvm中所选项目的特定值

从枚举绑定的 wpf 组合框中查找所选项目

以编程方式设置所选项目的颜色的组合框项目 WPF

在WPF中显示所选列表框项目的数据

wpf获取列表视图中所选项目的屏幕位置

WPF绑定到集合中所有项目的属性

WPF Textblock文本不会在组合框所选项目上动态更改

在ListView中获取所选项目的信息-WPF / C#

WPF组合框项目更新

如何使用mvvm在WPF列表框中显示所选项目中的数据?

如何获取非绑定WPF组合框以显示所选项目,而不是{System.Windows.Controls.ComboBoxItem:

WPF ListView所选项目可见

如何在 WPF 中验证 DataGrid 所选项目?

如何在WPF中实现图像轮播,其中所选项目始终是第一个?

组合框WPF C#中项目的显示不完整

如何对WPF组合框项目(与可观察的集合绑定)进行分类,并且每个类别必须显示不同的背景色

使用WPF中的Value从C#设置组合框项目

列表框项目WPF,不同项目的背景颜色不同

C#WPF如何修改ComboBox所选项目的颜色?

在复合集合中插入不同的项目类型

WPF:同步ItemsControl中所有项目的宽度

WPF ComboBox (with IsEditable) - 防止从选项列表中删除所选项目

所选项目ComboBox没有显示正确的ID WPF

C# WPF - 所选项目文本被剪切

WPF TreeView所选项目并显示用户控件

WPF在树视图中操纵所选项目

如何获取组合框中所选项目的ID并用它填充另一个组合框?

在组合框中获取所选项目

组合框中的所选项目消失