将标记绑定到视图模型属性
本文关键字:视图 模型 属性 绑定 | 更新日期: 2023-09-27 18:37:09
<DataTemplate x:Key="ItemTemplate">
<DockPanel Width="Auto">
<Button DockPanel.Dock="Top" Tag="{Binding id}">
<Button.Template>
<ControlTemplate >
<Image Source="{Binding image}"/>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Text="{Binding title}" HorizontalAlignment="Center" DockPanel.Dock="Bottom"/>
</DockPanel>
</DataTemplate>
<Grid x:Name="LeftGrid" Grid.Row="2" Grid.Column="0" >
<Border BorderThickness="1" BorderBrush="Red">
<ItemsControl ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding DisplayMovies.View}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</Grid>
我将每个Button
的Tag
值设置为 id
的值。每当选择Button
时,我都想将此Tag
值传递给ViewModel
中的property
。
有人可以帮我解决这个问题吗?我总是从ViewModel
绑定到XAML
,从不相反
我还应该提到Binding id
不是指我的ViewModel
。它指的是ItemsSource="{Binding DisplayMovies.View}"
中的property
你的意思是这样吗?
<Button
Command="{Binding DataContext.YourVmCommand,
RelativeSource={RelativeSource
AncestorType={x:Type ItemsControl}}}}"
CommandParameter="{Binding Id}"
DockPanel.Dock="Top" >