c# - 如何将附加属性绑定(bind)到 ComboBox 的 DisplayMemberPath?

标签 c# wpf binding

我似乎找不到正确的语法来允许将附加属性用作 ComboBox 的 DisplayMemberPath。

属性是SelectorSwitchedControl.NameForSelector
它位于映射到 XAML 前缀“local”的命名空间“LocalTest”中。

这是代码...

<UserControl x:Class="Playground.SelectorSwitchedControlTest.SelectorSwitchedControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:glc="clr-namespace:Playground.CommonControls"
    xmlns:local="clr-namespace:Playground.SelectorSwitchedControlTest"
    Background="Transparent">

    <Border x:Name="MainBorder"
        BorderBrush="Gray" BorderThickness="1">

        <DockPanel>

            <glc:FixedToolBar DockPanel.Dock="Top">

                <ComboBox x:Name="MainSelector"
                    ItemsSource="{Binding Children, ElementName=MainPanel}"
                    DisplayMemberPath="(local:SelectorSwitchedControl.NameForSelector)" />

            </glc:FixedToolBar>

            <local:SelectorSwitchedControlPanel x:Name="MainPanel" />

        </DockPanel>

    </Border>

</UserControl>

...由于某种原因,这给了我一个异常“前缀‘本地’未映射到命名空间。”我不确定为什么它说就像我删除了“DisplayMemberPath”行一样,“”标记的渲染就像它应该证明命名空间已映射一样。

我还尝试了以下所有方法...

  • DisplayMemberPath="local:SelectorSwitchedControl.NameForSelector"
  • DisplayMemberPath="(local:SelectorSwitchedControl.NameForSelector)"
  • DisplayMemberPath="SelectorSwitchedControl.NameForSelector"
  • DisplayMemberPath="(SelectorSwitchedControl.NameForSelector)"
  • DisplayMemberPath="LocalTest.SelectorSwitchedControl.NameForSelector"
  • DisplayMemberPath="(LocalTest.SelectorSwitchedControl.NameForSelector)"

我知道这只是我脑子不灵的日子之一,我错过了一些简单的事情,但这让我发疯!那么正确的语法是什么?

最佳答案

DisplayMemberPath - 每个项目的显示字符串属性的路径。将其设置为“NameForSelector”,而不是“{Binding NameForSelector}”。

 <DockPanel>
    <ComboBox x:Name="MainSelector" ItemsSource="{Binding Children}" DisplayMemberPath="NameForSelector" />
 </DockPanel>

 public class SelectorSwitchedControl
    {
        public string Name { get; set; }
        public string NameForSelector{ get; set; }
    }

关于c# - 如何将附加属性绑定(bind)到 ComboBox 的 DisplayMemberPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19693384/

相关文章:

c# - If 语句或拆分中的多个条件

c# - Xamarin : Segmented Control for windows uwp

c# - Wpf Prism - 将参数从 View 传递到 View 模型

WPF 工具包数据网格 : how to get ColumnHeader width to be the same as GridColumn width

c# - 将 View 绑定(bind)到 View 模型并在 Xamarin.Forms 中显示为对话框页面?

WPF 触发器绑定(bind)到 MVVM 属性

c# - 编译后创建继承

c# - 为什么我的 Windows 服务启动 csc.exe 的实例?

wpf - 互动EventTrigger中的事件名称为空

Silverlight - 将 Listbox ActualWidth 绑定(bind)到 ListboxItem 宽度