在WPF中更改组合框选择时的图像更改 [英] Image change when Combobox Selection Changed in WPF

查看:137
本文介绍了在WPF中更改组合框选择时的图像更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在另一个窗口(即窗口1)中基于组合框项目选择在主窗口中显示图像例如,我在Window1中拥有此组合框
在我的window1中使用组合框项目"on"和"off"时,如果要将组合框中的"on"选择为"on",将图像更改为主窗口的CB2,如果将其选择为"off",则要将其他图像更改为CB3
我如何在Wpf中做到这一点
这是我在window1.xmal
中的代码

How Do i Display Image in my main Window based on combobox Item Selection in another Window that is Window 1 For eaxmple Ihave This Comboboxin my Window1
with comboboxitems "on" and "off" in my window1 i want to change Image to CB2 of Main window if Selected "on" in combobox and other Image to CB3 if Selected "off"
how do i do that in Wpf
here is my code in window1.xmal

<ComboBox Height="21" HorizontalAlignment="Left" IsEditable="False" IsReadOnly="False" Margin="297,82,0,0" Name="comboBox13" VerticalAlignment="Top" Width="101" >
    <ComboBoxItem Content="ON" />
    <ComboBoxItem Content="OFF" />
</ComboBox>



这是我在MainWindow.Xmal中的图像



and here are my Images in MainWindow.Xmal

<Image Height="13" HorizontalAlignment="Left" Margin="284,236,0,0" Name="CB2" Source="/WpfApplication3;component/Images/blankSpacer.gif" />
<Image Height="13" HorizontalAlignment="Left" Margin="284,236,0,0" Name="CB3" Source="/WpfApplication3;component/Images/blankSpacer.gif" />

推荐答案

这里是个主意:

您可以将任何类型的项目添加到您的组合框. ComboBox中如何显示此类型取决于其object.ToString方法返回的内容.您可以重写此方法以返回所需的内容,例如"On"/"Off".让我们举个例子.

Here is the idea:

You can add to your combo box items of any type. How this type is presented in ComboBox depends on what its method object.ToString returns. You can override this method to return what you need, such as "On"/"Off". Let''s take your example.

struct SelectedItemHelper {
    internal SelectedItemHelper(bool value, string text) {
        Text = text; FValue = value;
    } //SelectedItemHelper

    public override string ToString() {
        return Text;
    } //ToString

    internal bool Value {
        get { return FValue; }
        set { FValue = value; }
    } //Value

    string Text;
    bool FValue;
} //SelectedItemHelper



要响应项目的更改,请处理事件SelectionChanged:



To respond to the change of the item, handle the event SelectionChanged:

ComboBox MyComboBox = new ComboBox();

//...

MyComboBox.Items.Add(new SelectedItemHelper(false, "Off"));
MyComboBox.Items.Add(new SelectedItemHelper(true, "On"));
MyComboBox.SelectedItem = 0; //or 1

MyComboBox.SelectionChanged += (sender, eventArgs) => {
    bool value = ((SelectedItemHelper)((ComboBox)sender).SelectedItem).Value;
    ShowPictire(value); //or whatever you want
}; //MyComboBox.SelectionChanged



这种方法非常通用且稳定:易于支持;如果您添加/删除/修改项目,则功能不会中断(与通过索引或字符串访问项目的情况相反).您可以将任何数据添加到帮助器类,而无论需要什么功能来支持应响应项目选择的功能. ListBoxTreeView几乎可以使用相同的技术.

—SA



This method is very universal and stable: it''s easy to support; functionality will not break if you add/delete/modify items (in contrast to case if you access items by index or string). You can add any data to the helper class, whatever you need to support functionality which should respond to item selection. Nearly the same techniques can be used for ListBox and TreeView.

—SA


这篇关于在WPF中更改组合框选择时的图像更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆