将 Obserable 集合绑定到 UWP 中的 MenuFlyoutSubItem
Bind Obserable Collection to MenuFlyoutSubItem in UWP
目前我得到"The property "项“没有可访问的setter。”我如何修改此控件以允许我将集合绑定到它并且可能只是将集合中对象的属性设置为项目的文本 属性?
<AppBarButton x:Name="Button" Icon="Add" Label="stuff">
<AppBarButton.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="b1" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b2" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b3" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b4" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b5" ></MenuFlyoutItem>
<MenuFlyoutSubItem x:Name="bb1" Items="{Binding MyList}" />
</MenuFlyout>
</AppBarButton.Flyout>
</AppBarButton>
你不能这样做。如果你看一下 at documentation,你会看到 Items 只有一个 getter,因此不能为其设置集合。您可以获得集合参考,然后向其中添加 Add 项目,但在显示弹出窗口之前,这似乎有效。之后没有应用任何更改(尽管我现在没有太多时间玩它)。这是一些代码,我在其中尝试了一些附加的 属性:
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="click">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="b1"/>
<MenuFlyoutItem Text="b2"/>
<MenuFlyoutSubItem Name="mysub" local:MenuExtension.MyItems="{Binding Options}" Text="Choosable items"/>
</MenuFlyout>
</Button.Flyout>
</Button>
<Button Content="modify" Click="Button_Click"/>
</StackPanel>
public static class MenuExtension
{
public static List<MenuFlyoutItem> GetMyItems(DependencyObject obj)
{ return (List<MenuFlyoutItem>)obj.GetValue(MyItemsProperty); }
public static void SetMyItems(DependencyObject obj, List<MenuFlyoutItem> value)
{ obj.SetValue(MyItemsProperty, value); }
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.Register("MyItems", typeof(List<MenuFlyoutItem>), typeof(MenuExtension),
new PropertyMetadata(new List<MenuFlyoutItem>(), (sender, e) =>
{
Debug.WriteLine("Filling collection");
var menu = sender as MenuFlyoutSubItem;
menu.Items.Clear();
foreach (var item in e.NewValue as List<MenuFlyoutItem>) menu.Items.Add(item);
}));
}
public sealed partial class MainPage : Page,INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public List<MenuFlyoutItem> Options { get; set; } = new List<MenuFlyoutItem>
{
new MenuFlyoutItem {Text="Start item" },
new MenuFlyoutItem {Text="Start item" },
new MenuFlyoutItem {Text="Start item" }
};
public MainPage()
{
this.InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Options = new List<MenuFlyoutItem> { new MenuFlyoutItem { Text = "Modified" } };
RaiseProperty(nameof(Options));
// mysub.Items.Add(new MenuFlyoutItem { Text = "modified" }); // even this cannot be done
}
}
也许你可以尝试一些模板,绑定按钮然后交换整个弹出按钮,或者尝试一些不同的东西
目前我得到"The property "项“没有可访问的setter。”我如何修改此控件以允许我将集合绑定到它并且可能只是将集合中对象的属性设置为项目的文本 属性?
<AppBarButton x:Name="Button" Icon="Add" Label="stuff">
<AppBarButton.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="b1" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b2" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b3" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b4" ></MenuFlyoutItem>
<MenuFlyoutItem Text="b5" ></MenuFlyoutItem>
<MenuFlyoutSubItem x:Name="bb1" Items="{Binding MyList}" />
</MenuFlyout>
</AppBarButton.Flyout>
</AppBarButton>
你不能这样做。如果你看一下 at documentation,你会看到 Items 只有一个 getter,因此不能为其设置集合。您可以获得集合参考,然后向其中添加 Add 项目,但在显示弹出窗口之前,这似乎有效。之后没有应用任何更改(尽管我现在没有太多时间玩它)。这是一些代码,我在其中尝试了一些附加的 属性:
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="click">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="b1"/>
<MenuFlyoutItem Text="b2"/>
<MenuFlyoutSubItem Name="mysub" local:MenuExtension.MyItems="{Binding Options}" Text="Choosable items"/>
</MenuFlyout>
</Button.Flyout>
</Button>
<Button Content="modify" Click="Button_Click"/>
</StackPanel>
public static class MenuExtension
{
public static List<MenuFlyoutItem> GetMyItems(DependencyObject obj)
{ return (List<MenuFlyoutItem>)obj.GetValue(MyItemsProperty); }
public static void SetMyItems(DependencyObject obj, List<MenuFlyoutItem> value)
{ obj.SetValue(MyItemsProperty, value); }
public static readonly DependencyProperty MyItemsProperty =
DependencyProperty.Register("MyItems", typeof(List<MenuFlyoutItem>), typeof(MenuExtension),
new PropertyMetadata(new List<MenuFlyoutItem>(), (sender, e) =>
{
Debug.WriteLine("Filling collection");
var menu = sender as MenuFlyoutSubItem;
menu.Items.Clear();
foreach (var item in e.NewValue as List<MenuFlyoutItem>) menu.Items.Add(item);
}));
}
public sealed partial class MainPage : Page,INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseProperty(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public List<MenuFlyoutItem> Options { get; set; } = new List<MenuFlyoutItem>
{
new MenuFlyoutItem {Text="Start item" },
new MenuFlyoutItem {Text="Start item" },
new MenuFlyoutItem {Text="Start item" }
};
public MainPage()
{
this.InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Options = new List<MenuFlyoutItem> { new MenuFlyoutItem { Text = "Modified" } };
RaiseProperty(nameof(Options));
// mysub.Items.Add(new MenuFlyoutItem { Text = "modified" }); // even this cannot be done
}
}
也许你可以尝试一些模板,绑定按钮然后交换整个弹出按钮,或者尝试一些不同的东西