将标记绑定到ViewModel属性

时间:2015-01-09 23:51:07

标签: c# wpf mvvm data-binding

<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>

我将每个Tag的{​​{1}}值设置为Button的值。每当选择id时,我都希望将此Button值传递给Tag中的property

有人可以帮助我解决这个问题吗?我总是从ViewModel绑定到ViewModel,而不是相反的

我还应该提到XAML并不是指Binding id。它指的是ViewModel

中的property

1 个答案:

答案 0 :(得分:1)

你的意思是这样吗?

 <Button 
    Command="{Binding DataContext.YourVmCommand, 
               RelativeSource={RelativeSource 
                   AncestorType={x:Type ItemsControl}}}}"
    CommandParameter="{Binding Id}"
    DockPanel.Dock="Top" >