Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
<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>


and here are my Images in MainWindow.Xmal
XML
<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" />
Posted
Updated 6-Mar-11 20:57pm
v3

1 solution

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.

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


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

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


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
 
Share this answer
 
v3
Comments
amitkarnik2211 7-Mar-11 1:56am    
Where Should I add this Struct to main window or other window Meathod
Sergey Alexandrovich Kryukov 7-Mar-11 3:25am    
What do you mean "where"? In .NET the order of type declaration does not matter. Inside you windows, for example. And the code where you use "+=" operation (adding the event handle, a second code fragment) should be called somewhere before a window is first shown. I like to do this in this way: override the virtual method "OnContentRendered" of my form and put all my initialization in this method at the end.

Is is clear now? Ask further questions in your comments if it is not, but better quickly prototype all that and see how it works first.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900