【问题标题】:(WPF) How to change button image background in listBox with buttons in each line ?(WPF)如何用每行按钮更改listBox中的按钮图像背景?
【发布时间】:2013-08-16 21:00:12
【问题描述】:

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

 <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

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

希望这一点清楚,请我花大约一天的时间来讨论这个问题 谁能帮忙

谢谢

【问题讨论】:

    标签: c# wpf listbox datatemplate


    【解决方案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>
    

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

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

    【讨论】:

    • 感谢 Sheridan,它可以进行一些更改,我明白了,使用切换按钮解决了,非常感谢 :)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      尝试使用 Togglebutton 代替按钮。使用触发器检查 toggleButton 的 IsChecked 属性何时更改。并据此改变你的形象。

      【讨论】:

      • -1 为什么要在 选择正确答案后添加答案,并且只提供该正确答案的 一小部分 信息你的答案?...完全没有意义。
      • 我没有看到答案已经给出。我继续接到打开此标签的电话,然后没有给出答案。回来后我发布了答案
      • +1 很公平。将来,可能在您发布答案之前更新页面?注意:在我被允许删除 -1 之前,我必须编辑您的答案(我只是添加了一个空格)。
      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2013-09-07
      • 2014-07-15
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      相关资源
      最近更新 更多