WPF 将路径绑定到类的类型名

发布于 2024-11-07 12:34:19 字数 1710 浏览 5 评论 0原文

使用 WPF,我想将 GroupBox 的标头绑定到多态类的类型名。因此,如果我有一个名为 Element 的类,以及两个从 Element 派生的类(例如 BasicElement 和 AdvancedElement),我希望 GroupBox 的标头显示“BasicElement”或“AdvancedElement”。这是我用于 GroupBox 的 xaml。它是 ItemsControl 使用的 DataTemplate 的一部分。我希望有一些东西可以代替 XAML 中的 Path=DerivedTypeNameOf(group) ,其中 group 是 groups 数组中的每个组。

请注意,TheData 的 ObjectInstance 被设置为 GroupSet 的有效实例,该实例包含一些 BasicGroup 和 AdvancedGroup 的数组。

以下是相关的代码隐藏类:

public class Group
{
    public string groupName;

    public string df_groupName
    {
        get { return this.groupName; }
        set { this.groupName = value; }
    }
}

public class BasicGroup : Group
{

}

public class AdvancedGroup : Group
{

}

public class GroupSet
{
    public Group [] groups;

    public Group [] df_groups
    {
        get { return this.groups; }
        set { this.groups = value; }
    }
};

这是 XAML:

<UserControl.Resources>
    <ResourceDictionary>
        <ObjectDataProvider x:Key="TheData" />

        <DataTemplate x:Key="GroupTemplate">
            <GroupBox Header="{Binding Path=DerivedTypeNameOf(group)}">
                <TextBox Text="This is some text"/>
            </GroupBox>
        </DataTemplate>

    </ResourceDictionary>
</UserControl.Resources>

    <ItemsControl ItemsSource="{Binding Source={StaticResource TheData}, Path=groups}" ItemTemplate="{StaticResource GroupTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

Using WPF, I want to bind the header of a GroupBox to the typename of a polymorphic class. So if I have a class called Element, and two classes that derive from Element, such as BasicElement and AdvancedElement, I want the header of the GroupBox to say "BasicElement" or "AdvancedElement". Here is the xaml I am using for the GroupBox. It's part of a DataTemplate being used by an ItemsControl. I'm hoping for something in place of Path=DerivedTypeNameOf(group) in the XAML, where group is each group in the groups array.

Note that the ObjectInstance of TheData is being set to a valid instance of GroupSet which holds an array of some BasicGroups and AdvancedGroups.

Here are the pertinent code-behind classes:

public class Group
{
    public string groupName;

    public string df_groupName
    {
        get { return this.groupName; }
        set { this.groupName = value; }
    }
}

public class BasicGroup : Group
{

}

public class AdvancedGroup : Group
{

}

public class GroupSet
{
    public Group [] groups;

    public Group [] df_groups
    {
        get { return this.groups; }
        set { this.groups = value; }
    }
};

Here's the XAML:

<UserControl.Resources>
    <ResourceDictionary>
        <ObjectDataProvider x:Key="TheData" />

        <DataTemplate x:Key="GroupTemplate">
            <GroupBox Header="{Binding Path=DerivedTypeNameOf(group)}">
                <TextBox Text="This is some text"/>
            </GroupBox>
        </DataTemplate>

    </ResourceDictionary>
</UserControl.Resources>

    <ItemsControl ItemsSource="{Binding Source={StaticResource TheData}, Path=groups}" ItemTemplate="{StaticResource GroupTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-11-14 12:34:19

您始终可以使用 ValueConverter 来获取类型:

public class TypeNameConverter : IValueConverter {
  public object Convert(object value, Type targetType,
  object parameter, CultureInfo culture) {
    return value.GetType().Name;
  }

  public object ConvertBack(object value, Type targetType,
  object parameter, CultureInfo culture) {
    throw NotImplementedException();
  }
}

这将允许您在集合中拥有任何类型,而不需要它实现属性来获取值。否则,只需按照 David 所说的操作并实现一个属性即可给出结果。如果存在来自基类的一般继承,您甚至不需要在每个类中实现它。只需使用 GetType().Name 在基础中实现它,您将始终获得正确的值。

You could always use a ValueConverter to get the type:

public class TypeNameConverter : IValueConverter {
  public object Convert(object value, Type targetType,
  object parameter, CultureInfo culture) {
    return value.GetType().Name;
  }

  public object ConvertBack(object value, Type targetType,
  object parameter, CultureInfo culture) {
    throw NotImplementedException();
  }
}

That would allow you to have any type in your collection without any need for it to implement a property to get the value. Otherwise, just do as David says and implement a property to give the result. You wouldn't even need to implement it in every class if there is general inheritance from a base class. Just implement it in the base with GetType().Name and you'll always get the correct value.

烟火散人牵绊 2024-11-14 12:34:19

为什么不直接添加

public abstract string DerivedTypeName { get; set; }

到您的基类并为每个派生类型重写它,然后您只需绑定到一个字符串即可。

Why not just add

public abstract string DerivedTypeName { get; set; }

to your base class and override it for each derived type then you are simply binding to a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文