【问题标题】:Button doesn't work in FlipView with DataTemplates - WinRT按钮在带有 DataTemplates 的 FlipView 中不起作用 - WinRT
【发布时间】:2015-08-17 12:01:38
【问题描述】:

我正在使用 Flipview 和 DataTemplateSelector 来确定在运行时应用哪个 DataTemplate 来显示我的控件中的项目。

我有两个 DataTemplate,一个是静态的,第二个可以用于不确定数量的项目。

问题是按钮什么都不做。我在SaveCommand 中使用了断点,但是当我单击按钮时,它不会中断。

XAML

<Page.Resources>
    <DataTemplate x:Key="FirstDataTemplate">
        <Grid>
            <TextBlock Text="{Binding Content}" Margin="10,0,18,18"></TextBlock>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="SecondDataTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"></ColumnDefinition>
                <ColumnDefinition Width="auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Text="{Binding Url}"></TextBox>
            <Button Grid.Column="1" Name="SendButton" 
                Style="{StaticResource ImageButtonStyle}" 
                Command="{Binding Path=SaveCommand}"
                HorizontalAlignment="Center">
                <Grid>
                    <Image Source="ms-appx:///Skins/Images/buton.png" Stretch="None" />
                    <TextBlock Text="CLICK ME" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White"/>
                </Grid>
            </Button>
        </Grid>
    </DataTemplate>
    <local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
        FirstTextTemplate="{StaticResource FirstDataTemplate}"
    SecondTextTemplate="{StaticResource SecondDataTemplate}">
    </local:MyDataTemplateSelector>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <FlipView x:Name="itemGridView" ItemTemplateSelector="{StaticResource MyDataTemplateSelector}" 
        Margin="265,220,284,162">
    </FlipView>
</Grid>

代码隐藏

public sealed partial class FlipViewDemo : Page
{
    public FlipViewDemo()
    {
        this.InitializeComponent();

        var items = new List<BaseClass>();

        items.Add(new FirstItem
        {
            Content="This is a test - Content"
        });

        for (int i = 0; i < 18; i++)
        {
            items.Add(new SecondItem
            {
                Url = "http://www.google.com/ " + i.ToString() 
            });
        }
        itemGridView.ItemsSource = items;
    }
}

public class BaseClass
{

}

public class FirstItem : BaseClass
{
    public string Content { get; set; }
}

public class SecondItem : BaseClass
{
    public string Url { get; set; }
}

public class MyDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate FirstTextTemplate { get; set; }
    public DataTemplate SecondTextTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        if (item is FirstItem)
            return FirstTextTemplate;
        if (item is SecondItem)
            return SecondTextTemplate;

        return base.SelectTemplateCore(item, container);
    }
}

视图模型

public class FisaObsViewModel : ViewModelBase
{
    private RelayCommand saveCommand;

    public FisaObsViewModel()
    {

    }

    public RelayCommand SaveCommand
    {
        get
        {
            return saveCommand ?? (saveCommand = new RelayCommand(
                async () =>
                {
                    try
                    {
                        MessageDialog dlg = new MessageDialog("Message");
                        await dlg.ShowAsync();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }));
        }
    }
}

我检查过的一些链接:

http://www.mutzl.com/tag/mvvm-light/

http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

http://social.technet.microsoft.com/wiki/contents/articles/18199.event-handling-in-an-mvvm-wpf-application.aspx

【问题讨论】:

    标签: c# wpf xaml mvvm windows-runtime


    【解决方案1】:

    问题在于DataTemplate 引用的是您的Model,而不是您的ViewModel。因此,您的命令绑定正在尝试在 Model 上查找命令。

    您需要将绑定源更改为将 DataContext 设置为 ViewModel 的任何元素。

    {Binding DataContext.SaveCommand, RelativeSource={RelativeSource AncestorType=Page}}
    

    或者

    给您的Page 一个Name,并使用以下绑定:

    {Binding DataContext.SaveCommand, ElementName=myPageName}
    

    【讨论】:

    • 您好,感谢您的回复。我不确定应该在哪里使用您的代码。我应该替换“命令”中的绑定吗?
    • 我得到这个错误:Error 1 The member "AncestorType" is not recognized or is not accessible. Error 2 The property 'AncestorType' was not found in type 'RelativeSource'. Error 3 Unknown member 'AncestorType' on element 'RelativeSource'. 我只有DispatcherModeRelativeSource
    • 我已编辑我的答案以提供替代解决方案。我认为可能是因为您使用的是 WinRT,它可能没有 RelativeSource(但我不确定)。
    猜你喜欢
    • 2013-08-12
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多