【发布时间】:2013-07-22 13:46:43
【问题描述】:
我有一个 gridView。
它有一个 Itemptemplate (textblock, image) 和 itemsource。 textBlock 和 Image 绑定到 itemsource。
我想向 ItempTemplate 添加一个按钮,但我无法检测到 eventHandler。
在我的 .cs 文件中,我没有看到文本块、图像或按钮。
如何设置事件,
这里是项目模板的代码
<DataTemplate x:Key="IDViewStyle">
<Grid Width="350" Height="450" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid>
<Border Background="#B2060606" />
<Button HorizontalAlignment="Right" BorderThickness="0" x:Name="eraseButton" VerticalAlignment="Top">
<Image Source="/Assets/Images/erease.png" Width="90" Margin="0,-7,-15,15"/>
</Button>
<StackPanel Margin="0" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="+" VerticalAlignment="Center" Style="{StaticResource PageHeaderTextStyle}" FontSize="160" Margin="0"/>
<TextBlock TextWrapping="Wrap" Text="Ekle" TextAlignment="Center" Style="{StaticResource HeaderTextStyle}"/>
</StackPanel>
<Image Stretch="Fill" Source="{Binding Image}"/>
</Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding Type}" VerticalAlignment="Top" Grid.Row="1" Style="{StaticResource SubheaderTextStyle}" TextAlignment="Center"/>
</Grid>
</DataTemplate>
还有我的 .cs 文件
Data.IdentityTypeCollection collection;
gView.SelectionChanged += lvIdTypes_SelectionChanged;
collection = new Data.IdentityTypeCollection();
gView.ItemsSource = collection;
gView.ScrollIntoView(collection);
还有我的 mainpage.xaml
<GridView x:Name="gView" Grid.Row="1" Grid.RowSpan="2" Margin="117,0,0,100" ItemTemplate="{StaticResource IDViewStyle}"/>
如何在项目模板中使用按钮事件
【问题讨论】:
-
发布代码会有所帮助
-
您可能需要将按钮绑定到视图模型上的命令。就像@makc 说的那样,代码会让你更容易弄清楚如何提供帮助。
-
我已经发布了代码
-
您是否尝试添加点击事件 Click="btn_Click"?
-
您可以通过添加
Click="YourEventNameHere"在xaml中设置事件。此点击事件对于您的所有 GridView 项目将是通用。
标签: c# xaml microsoft-metro