WPF ListView按钮 - 关联多个图像

时间:2010-06-15 16:12:15

标签: wpf xaml listview

我在WPF应用程序中有一个listview,它的一个列中有一个按钮。我将图像关联到按钮控件。我想根据这个按钮为这个按钮配置不同的图像 如何在这种情况下编写XAML ???

这就是我已经拥有的:

<ControlTemplate x:Key="IconButtonAddProfile" TargetType="{x:Type Button}">
  <Grid>
    <Image x:Name="myimage" Source="rofile.png" Height="27" Width="65" />
    <Border>
      <ContentPresenter Content="{TemplateBinding Content}"/>
    </Border>
  </Grid>
</ControlTemplate>

<ListView.View>
  <GridView>
    <GridViewColumn Header="Security" Width="50">
      <GridViewColumn.CellTemplate>
        <DataTemplate>
          <Button Template="{StaticResource IconButtonLockSecure}"
                  DataContext="{Binding}" MinHeight="20" MinWidth="50" />
        </DataTemplate>
      </GridViewColumn.CellTemplate>
    </GridViewColumn>
</ListView.View>

2 个答案:

答案 0 :(得分:0)

假设你的模型有一个属性“ButtonImage”,它返回对象图像的ImageSource,以及一个属性“ButtonContent”,它返回按钮的内容(文本或其他)。

然后您的ControlTemplate可以是:

<ControlTemplate x:Key="IconButtonAddProfile" TargetType="{x:Type Button}"> 
  <Grid> 
    <Image x:Name="myimage" Source="{Binding ButtonImage}" /> 
    <Border> 
      <ContentPresenter Content="{Binding ButtonContent}"/> 
    </Border> 
  </Grid> 
</ControlTemplate> 

请注意,您也不需要在Button上设置DataContext,因为它是继承的。

答案 1 :(得分:0)

我添加了一个返回硬编码图像路径的属性ButtonImage。         public string ButtonImage         {             得到             {                 return @“Images / glowLine.png”;             }         } 我按如下方式配置了ListView列。

                <GridViewColumn Header="Add Profile">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button  Template="{Binding IconButtonAddProfile}" Click="Button_Click" DataContext="{Binding}" MinHeight="20" MinWidth="50" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

但是图片没有显示在列表视图中