在 XAML 中绑定静态泛型 Class
Binding a Static Generic Class in XAML
我正在尝试实现在此 post 上找到的 MenuExtension class 的通用版本:。当明确定义有问题的 MenuFlyoutItem 类型时,MenuExtension class 工作正常,但是,我希望能够将 class 用于 MenuFlyoutItems、RadioMenuFlyoutItems 等列表,而无需定义版本对于每种类型。
到目前为止,我有 Romasz 的 MenuExtension 的通用版本
public static class MenuExtension<T> where T : MenuFlyoutItemBase
{
public static List<T> GetMyItems(DependencyObject obj)
{ return (List<T>)obj.GetValue(MyItemsProperty); }
public static void SetMyItems(DependencyObject obj, List<T> value)
{ obj.SetValue(MyItemsProperty, value); }
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.Register("MyItems", typeof(List<T>), typeof(MenuFlyoutItemExtension<T>),
new PropertyMetadata(new List<T>(), (sender, e) =>
{
Debug.WriteLine("Filling collection");
var menu = sender as MenuFlyoutSubItem;
menu.Items.Clear();
foreach (var item in e.NewValue as List<T>) menu.Items.Add(item);
}));
}
这是使用它的 XAML 代码(在使用 MenuExtension 的原始版本时有效)
<Button.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="An Item" />
<MenuFlyoutSubItem
Text="Sub menu"
local:MenuExtension.MyItems="{x:Bind local:GradeWeightsFlyoutItems.GetMenuItems(_currItem)}"/>
</MenuFlyout>
</Button.ContextFlyout>
我不知道 local:MenuExtension.MyItems
在这种情况下是如何工作的,所以这可能是其中的一部分。
为了以防万一,下面是获取菜单项的 C# 函数:
static class GradeWeightsFlyoutItems
{
public static List<MenuFlyoutItem> GetMenuItems(Grade grade)
{
return grade.weights.Select(weight =>
{
var flyout = new MenuFlyoutItem
{
Text = weight.Name,
};
// Other code here left out for simplicity
return flyout;
}).ToList();
}
}
恐怕 XAML 不支持泛型:https://github.com/microsoft/microsoft-ui-xaml/issues/931。
如果您希望能够将附加的 属性 设置为不同类型的集合和序列,您可以将 List<T>
更改为 IEnumerable
。
我正在尝试实现在此 post 上找到的 MenuExtension class 的通用版本:
到目前为止,我有 Romasz 的 MenuExtension 的通用版本
public static class MenuExtension<T> where T : MenuFlyoutItemBase
{
public static List<T> GetMyItems(DependencyObject obj)
{ return (List<T>)obj.GetValue(MyItemsProperty); }
public static void SetMyItems(DependencyObject obj, List<T> value)
{ obj.SetValue(MyItemsProperty, value); }
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.Register("MyItems", typeof(List<T>), typeof(MenuFlyoutItemExtension<T>),
new PropertyMetadata(new List<T>(), (sender, e) =>
{
Debug.WriteLine("Filling collection");
var menu = sender as MenuFlyoutSubItem;
menu.Items.Clear();
foreach (var item in e.NewValue as List<T>) menu.Items.Add(item);
}));
}
这是使用它的 XAML 代码(在使用 MenuExtension 的原始版本时有效)
<Button.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="An Item" />
<MenuFlyoutSubItem
Text="Sub menu"
local:MenuExtension.MyItems="{x:Bind local:GradeWeightsFlyoutItems.GetMenuItems(_currItem)}"/>
</MenuFlyout>
</Button.ContextFlyout>
我不知道 local:MenuExtension.MyItems
在这种情况下是如何工作的,所以这可能是其中的一部分。
为了以防万一,下面是获取菜单项的 C# 函数:
static class GradeWeightsFlyoutItems
{
public static List<MenuFlyoutItem> GetMenuItems(Grade grade)
{
return grade.weights.Select(weight =>
{
var flyout = new MenuFlyoutItem
{
Text = weight.Name,
};
// Other code here left out for simplicity
return flyout;
}).ToList();
}
}
恐怕 XAML 不支持泛型:https://github.com/microsoft/microsoft-ui-xaml/issues/931。
如果您希望能够将附加的 属性 设置为不同类型的集合和序列,您可以将 List<T>
更改为 IEnumerable
。