【发布时间】:2019-09-10 19:40:27
【问题描述】:
我无法将 MenuFlyoutItem 的命令绑定到我的 ViewModel 上的 RelayCommand。我已经尝试了所有我能想到的方法,ElementName、RelativeSource 等。谁能告诉我我做错了什么?下面代码中显示的其他两个绑定有效。只有命令绑定没有。我的意思是我在由 RelayCommand 调用的 OnFilterListCommand 方法中设置了一个断点。当我单击菜单弹出项时,执行永远不会到达该断点。
<wct:DataGridComboBoxColumn.HeaderStyle>
<Style TargetType="controlsprimitives:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<TextBlock Text="Company" TextWrapping="Wrap"/>
<Button HorizontalAlignment="Right" x:Name="MyButton" Content="test">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:InvokeCommandAction Command="{Binding ElementName=thePage, Path=DataContext.OpenFlyoutCommand}" CommandParameter="{Binding ElementName=MyButton}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<FlyoutBase.AttachedFlyout>
<Flyout helpers:BindableFlyout.ItemsSource="{Binding ElementName=theView, Path=DataContext.SourceForCompaniesList}" x:Name="theFlyout">
<helpers:BindableFlyout.ItemTemplate>
<DataTemplate>
<MenuFlyoutItem Text="{Binding CompanyName}" Command="{Binding Path=DataContext.FilterListCommand, ElementName=theView}" IsTapEnabled="True"/>
</DataTemplate>
</helpers:BindableFlyout.ItemTempla
</Flyout>
</FlyoutBase.AttachedFlyout>
这是来自 ViewModel 的适用代码。
private RelayCommand<object> _filterListCommand;
public RelayCommand<object> FilterListCommand => _filterListCommand
?? (_filterListCommand = new RelayCommand<object>(OnFilterListCommand));
private void OnFilterListCommand(object obj)
{
string selectedCompany = obj as string;
...
}
我正在使用 Jerry Nixon 的解决方案将 ItemsSources 属性添加到 FlyoutMenu:
public class BindableFlyout : DependencyObject
{
#region ItemsSource
public static IEnumerable GetItemsSource(DependencyObject obj)
{
return obj.GetValue(ItemsSourceProperty) as IEnumerable;
}
public static void SetItemsSource(DependencyObject obj, IEnumerable value)
{
obj.SetValue(ItemsSourceProperty, value);
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.RegisterAttached("ItemsSource", typeof(IEnumerable),
typeof(BindableFlyout), new PropertyMetadata(null, ItemsSourceChanged));
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ Setup(d as Windows.UI.Xaml.Controls.Flyout); }
#endregion
#region ItemTemplate
public static DataTemplate GetItemTemplate(DependencyObject obj)
{
return (DataTemplate)obj.GetValue(ItemTemplateProperty);
}
public static void SetItemTemplate(DependencyObject obj, DataTemplate value)
{
obj.SetValue(ItemTemplateProperty, value);
}
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.RegisterAttached("ItemTemplate", typeof(DataTemplate),
typeof(BindableFlyout), new PropertyMetadata(null, ItemsTemplateChanged));
private static void ItemsTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ Setup(d as Windows.UI.Xaml.Controls.Flyout); }
#endregion
private static async void Setup(Windows.UI.Xaml.Controls.Flyout m)
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
return;
var s = GetItemsSource(m);
if (s == null)
return;
var t = GetItemTemplate(m);
if (t == null)
return;
var c = new Windows.UI.Xaml.Controls.ItemsControl
{
ItemsSource = s,
ItemTemplate = t,
};
var n = Windows.UI.Core.CoreDispatcherPriority.Normal;
Windows.UI.Core.DispatchedHandler h = () => m.Content = c;
await m.Dispatcher.RunAsync(n, h);
}
}
我在这里找到了:http://blog.jerrynixon.com/2013/12/xaml-how-to-add-itemssource-to-windows.html
【问题讨论】:
-
当我使用命令绑定时,它运行良好。你能展示更多关于 RelayCommand 类的代码吗?
-
谢谢费旺。我从我试图绑定的 RelayCommand 添加了代码。我开始怀疑 BindableFlyout 类是否有一些我不理解的地方。我也添加了。
-
从您的代码中,您的 FilterListCommand 和 SourceForCompaniesList 在同一个视图模型中吗?您应该将 FilterListCommand 和 CompanyName 放入同一模型中。另外,能否提供一个样本让我们复现这个问题?
-
是的,SourceForCompaniesList 和 FilterListCommand 都在同一个 ViewModel 中。顺便说一句,我正在尝试做的是在gridview的列标题中放置一个下拉列表,以便用户可以像在Excel中一样过滤列。
标签: c# xaml uwp mvvm-light