首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >APP.xaml中事件的XAML外包数据板

APP.xaml中事件的XAML外包数据板
EN

Stack Overflow用户
提问于 2016-04-04 09:07:38
回答 3查看 675关注 0票数 1

我有一个ListViewItemTemplate的工作数据板

代码语言:javascript
复制
<ListView ItemTemplate="{StaticResource MYTEMPLATE}"             
          HorizontalAlignment="Center"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
          ContinuumNavigationTransitionInfo.IsEntranceElement="True">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel VerticalAlignment="Bottom"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

模板看起来是这样的:

代码语言:javascript
复制
<DataTemplate x:Key="GlobalBox">
        <Border Background="#FFFFFFFF" Margin="10 0 0 5" CornerRadius="2 2 15 2">
            <Grid Width="380">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Tag="{Binding ID}" Name="ProfileInfo" Tapped="Profile_Tapped" Orientation="Horizontal" Margin="15 15 15 0">
                    <Grid Width="360" Margin="0 0 0 10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="75"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="50"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" Height="45" Width="45" CornerRadius="5">
                            <Border.Background>
                                <ImageBrush ImageSource="{Binding ImagePath}" Stretch="UniformToFill"/>
                            </Border.Background>
                        </Border>
                        <StackPanel Grid.Column="1" Orientation="Vertical" Margin="0 5 0 0">
                            <TextBlock Text="{Binding Name}" Foreground="Black" FontSize="18" FontWeight="Bold"></TextBlock>
                            <TextBlock Text="{Binding Handle}" Foreground="DarkGray" FontSize="12"/>
                        </StackPanel>
                        <Image Grid.Column="2" Source="Assets/ActionIcons/logo_blue_32.png" Width="32" VerticalAlignment="Top"></Image>
                    </Grid>
                </StackPanel>
                <StackPanel  Grid.Row="1" Margin="14.5,0,0,0" Height="Auto">
                    <StackPanel Name="TweetContent" Tag="{Binding ID}" Margin="15 0 15 0" Tapped="Content_Tapped">
                        <TextBlock Text ="{Binding Content}" TextWrapping="Wrap" Foreground="Black" FontSize="14" Margin="0 0 0 10"/>
                        <ItemsControl  ItemsSource="{Binding ContentImages}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Image Source="{Binding }" MaxWidth="350" Margin="0 0 0 5"  HorizontalAlignment="Center"></Image>
                                    </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <TextBlock Foreground="DarkGray" Text="{Binding DateSend}" FontSize="10"></TextBlock>
                    </StackPanel>
                    <StackPanel Name="ActionButtons">
                        <Grid Tag="{Binding ID}" Width="380" Height="25" Margin="0 0 0 10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            <Button Grid.Column="0" HorizontalAlignment="Center" Margin="20 0 0 0" Style="{StaticResource replyActionButton}" Tapped="Reply_Tapped"></Button>

                            <ToggleButton Grid.Column="2" HorizontalAlignment="Center" 
                                          Style="{StaticResource retweetActionButton}" 
                                          Tapped="Retweet_Tapped"></ToggleButton>
                            <TextBlock Grid.Column="3" HorizontalAlignment="Left" Margin="-15 0 0 0" VerticalAlignment="Center" Text="{Binding RetweetCount}" Foreground="DarkGray"/>

                            <ToggleButton Grid.Column="4" HorizontalAlignment="Center" 
                                          Style="{StaticResource likeActionButton}" 
                                          IsChecked="{Binding LikeState}"  
                                          Tapped="Favourite_Tapped"></ToggleButton>
                            <TextBlock Grid.Column="5"  HorizontalAlignment="Left" Margin="-15 0 0 0" VerticalAlignment="Center" Text="{Binding LikeCount}" Foreground="DarkGray"/>
                        </Grid>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </Border>
    </DataTemplate>

现在,当我将模板放在app.xaml文件中时,我将得到以下编译错误

无法在应用程序类XAML文件中设置事件

这对我来说是有道理的,但我怎么做呢?我是否可以将不同的事件(如变量或其他事件)传递到数据板中?

//UPDATE -使用USERCONTROL的解决方案WAY2:

我用上面的代码做了一个UserControl,并在ListView中实现了它。

代码语言:javascript
复制
<ListView Grid.Row="1" Margin="0 0 0 5"       
        x:Name = "standardBox"
        HorizontalAlignment="Center"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        ContinuumNavigationTransitionInfo.IsEntranceElement="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <local:UCGlobal></local:UCGlobal>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel VerticalAlignment="Bottom"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-04 10:07:42

您可以使用以下几种选择:

  1. 使用模板中按钮的命令属性(注意DataContext设置正确,以避免绑定错误):

如果它是另一种类型的不调用命令的事件,您可以使用InvokeCommandAction,它将被处理,如下一个示例所示。

  1. 使用类似于CallMethodAction的触发器(与上面相同)。DataContext是搜索方法的地方。): .
  2. 编写一个小UserControl作为DataTemplate的基础

XAML:

代码语言:javascript
复制
<UserControl x:Class="ThreeBtnUserCtrl"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:DataTemplateIssue"
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
       <Button x:Name="button1" Content="Button" Click="button1_Click"/>
       <Button x:Name="button2" Content="Button" Click="button2_Click"/>
       <Button x:Name="button3" Content="Button" Click="button3_Click"/>
    </StackPanel>
</UserControl>

代码背后:

代码语言:javascript
复制
public partial class ThreeBtnUserCtrl : UserControl
{
    public ThreeBtnUserCtrl()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
        //...some code only controlling view related stuff of your UserCtrl
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        //...some code only controlling view related stuff of your UserCtrl
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        //...some code only controlling view related stuff of your UserCtrl
    }
}

在你的DataTemplate中使用它

代码语言:javascript
复制
<DataTemplate x:Key="MyTemplate">
    <Grid>
        <local:ThreeBtnUserCtrl/>
    </Grid>
</DataTemplate>
票数 2
EN

Stack Overflow用户

发布于 2016-04-04 10:01:47

您可以使用CommandEventTrigger并将其绑定到视图模型的命令中。

代码语言:javascript
复制
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Tapped">
        <i:InvokeCommandAction Command="{Binding TappedCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

或者在UserControl中使用小的DataTemplate

代码语言:javascript
复制
<UserControl... >
    <StackPanel>
       <Button x:Name="button1" Content="Button" Click="button1_Click"/>
    </StackPanel>
</UserControl>

并在您的app.xaml中使用它:

代码语言:javascript
复制
<DataTemplate x:Key="Template">
    <Grid>
        <local:myUserControl/>
    </Grid>
</DataTemplate>
票数 1
EN

Stack Overflow用户

发布于 2016-04-06 13:57:12

在堆栈面板中使用行为

代码语言:javascript
复制
<DataTemplate x:Key="GlobalBox">
    <Border Background="#FFFFFFFF" Margin="10 0 0 5" CornerRadius="2 2 15 2">
        <Grid Width="380">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Tag="{Binding ID}" Name="ProfileInfo" Orientation="Horizontal" Margin="15 15 15 0">
                <Grid Width="360" Margin="0 0 0 10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="75"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="50"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" Height="45" Width="45" CornerRadius="5">
                        <Border.Background>
                            <ImageBrush ImageSource="{Binding ImagePath}" Stretch="UniformToFill"/>
                        </Border.Background>
                    </Border>
                    <StackPanel Grid.Column="1" Orientation="Vertical" Margin="0 5 0 0">
                        <TextBlock Text="{Binding Name}" Foreground="Black" FontSize="18" FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding Handle}" Foreground="DarkGray" FontSize="12"/>
                    </StackPanel>
                    <Image Grid.Column="2" Source="Assets/ActionIcons/logo_blue_32.png" Width="32" VerticalAlignment="Top"></Image>
                </Grid>
                <interact:Interaction.Behaviors>
                    <interactcore:EventTriggerBehavior EventName="Tapped" >
                        <interactcore:InvokeCommandAction Command="{Binding Path=DataContext.Btn1Command}" />
                    </interactcore:EventTriggerBehavior>
                </interact:Interaction.Behaviors>
            </StackPanel>

        </Grid>
    </Border>
</DataTemplate>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36398535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档