ICommand将转到itemsource而不是datacontext

时间:2015-04-01 04:45:03

标签: c# wpf xaml mvvm icommand

我知道我的命令,但找不到合适的路径。

Error: BindingExpression path error: 'OnButtonClickedCommand' property not found on 'App1.Helper.FoodTypes, App1.Windows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='OnButtonClickedCommand' DataItem='App1.Helper.FoodTypes, App1.Windows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.Button' (Name='button1'); target property is 'Command' (type 'ICommand')

下面我使用items source中的元素来设置标记和内容。但是我希望我发送的命令被发送到datacontext。但相反,它被发送到同一个元素。

MenuItemsPage.xaml

<DataTemplate x:Key="foodTypeTemplate">
    <Button x:Name="button1" Width="100" Height="50" Content="{Binding Name}" Tag="{Binding Name}" 
              Command="{Binding OnButtonClickedCommand}"                   
            CommandParameter="{Binding ElementName=button1}"/>
</DataTemplate>

我知道该命令将转到项目源(CategoryButtonList)的元素,因为如果我用名称替换 OnButtonClickedCommand ,则会找到该路径。相反,我希望它转到我在 cs 文件中设置的datacontext。根据我的研究,您应该将RelativeResource与FindAncestorType一起使用,但我的平台似乎不支持它。

Error   3   The property 'AncestorType' was not found in type 'RelativeSource'.

我希望它转到datacontext的原因是因为这就是所有逻辑的所在。

以下是我的ItemsSource设置方法

MenuItemsPage.xaml

    <ListBox HorizontalAlignment="Left" Height="425" Margin="75,140,0,0" VerticalAlignment="Top" Width="105"
             ItemsSource="{Binding CategoryButtonList}" ItemTemplate="{StaticResource foodTypeTemplate}" >
    </ListBox>

在我的MenuItemsPage中,我在代码中设置了DataContext。也许我应该在xaml中设置它以保持我的 cs 清洁?

MenuItemsPage.xaml.cs

public sealed partial class MenuItemsPage : Page
    {
        public MenuItemsPage()
        {
            this.InitializeComponent();                   
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string tableName = e.Parameter as string;
            this.DataContext = new MenuPageVM(tableName);        
        }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用“ElementName”和“DataContext.OnButtonClickedCommand”来引用ListBox的datacontext

<ListBox x:Name ="CategoryListBox" HorizontalAlignment="Left" Height="425" Margin="75,140,0,0" VerticalAlignment="Top" Width="105"
             ItemsSource="{Binding CategoryButtonList}" ItemTemplate="{StaticResource foodTypeTemplate}" >
    </ListBox>


<DataTemplate x:Key="foodTypeTemplate">
    <Button x:Name="button1" Width="100" Height="50" Content="{Binding Name}" Tag="{Binding Name}" 
              Command="{Binding DataContext.OnButtonClickedCommand, ElementName = CategoryListBox}"                   
            CommandParameter="{Binding ElementName=button1}"/>
</DataTemplate>

答案 1 :(得分:1)

DataContext {及其所有子级,例如DataTemplateButton 实际ItemsSource ListBox 1}}。如果该命令是在窗口的DataContext 中定义的,则使用ElementNameRelativeSource

<Window x:Name="MyWindow" ... >
    <ListBox ItemsSource="...">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding ElementName=MyWindow, Path=DataContext.MyCommand}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

<Window ... >
    <ListBox ItemsSource="...">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyCommand}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>