2

我有列表框,数据模板是按钮:

 <ListBox  x:Name="lbname" ItemsSource="{Binding myCollection}">        
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button x:Name="btnname" Content="{Binding name}" Click="btnname_Click">
                    <Button.Background>
                        <ImageBrush ImageSource="/myApplication;component/images/buttons/normal.png"/>
                    </Button.Background>
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我有两个用于此列表框图像的图像背景(normal.png 用于正常模式,click.png 用于列表框中的选定项目)

按钮列表中的listBox视图项,按钮图片背景一般为normal.png

我的问题是如何将按钮图像背景更改为 click.png 以选择一个并将旧的选定按钮检索为 normal.png

如何使用每行中的按钮更改列表框中所选项目的图像背景?

希望这很清楚,请我花大约一天的时间来解决这个问题,任何人都可以帮助

谢谢

4

3 回答 3

1

我还没有测试它,但你需要一些看起来像这样的代码:

<ListBox x:Name="lbname" ItemsSource="{Binding myCollection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button x:Name="btnname" Click="btnname_Click">
                <Grid>
                    <Image>
                        <Image.Style>
                            <Style>
                                <Setter Property="Image.Source" 
Value="/myApplication;component/images/buttons/normal.png" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsSelected, 
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" 
Value="True">
                                        <Setter Property="Image.Source" 
Value="/myApplication;component/images/buttons/click.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <TextBlock Text="{Binding name}" HorizontalAlignment="Center" 
VerticalAlignment="Center" />
                </Grid>
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这个想法是直接绑定到对象的IsSelected属性ListBoxItem。这是使用RelativeSource绑定完成的。但是,我猜这段代码并不能满足您的要求...我建议您可能要使用 aToggleButton代替...像这样:

<ListBox x:Name="lbname" ItemsSource="{Binding myCollection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ToggleButton x:Name="btnname" Click="btnname_Click" IsChecked="{Binding 
IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type 
ListBoxItem}}}">
                <Grid>
                    <Image>
                        <Image.Style>
                            <Style>
                                <Setter Property="Image.Source" 
Value="/myApplication;component/images/buttons/normal.png" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsSelected, 
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" 
Value="True">
                                        <Setter Property="Image.Source" 
Value="/myApplication;component/images/buttons/click.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <TextBlock Text="{Binding name}" HorizontalAlignment="Center" 
VerticalAlignment="Center" />
                </Grid>
            </ToggleButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
于 2013-08-14T10:15:15.020 回答
0

如果您使用的是 MVVM,则在 viewModel 中创建一个属性并将其作为 ImageSource 绑定到 ImageBrush,当您在 ListView 中选择值时,获取所选记录并更改此属性的图像路径。

于 2013-08-14T10:16:13.710 回答
0

尝试使用切换按钮而不是按钮。使用触发器检查切换按钮的 IsChecked 属性何时更改。并在此基础上改变你的形象。

于 2013-08-14T10:45:20.500 回答