Click here to Skip to main content
14,732,999 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I have dat grid with five column Icon,Position,name.... On click of button if the Position is 2 then i want change the image to online.png..can any one help me..
my code is..i am taking value from another window for position...value is perfectly pass from one window to another..but getting difficulty in changing the image ..
window1.xaml
<pre lang="xml"><Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="356" Width="477" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Window_Loaded">
    <Grid>
        <my:DataGrid  AutoGenerateColumns="False" Margin="36,23,45,112" Name="dataGrid1" Grid.Row="1" Background="White" VerticalGridLinesBrush="DarkGray" VerticalScrollBarVisibility="Hidden" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"  >
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn Header="Icon" Width="50" IsReadOnly="True" >
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=ImgPath}" Width="20" Height="20"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
                <my:DataGridTextColumn Header="Position"  Binding="{Binding Path=PO}"   />
                <my:DataGridTextColumn Header=" Name" Binding="{Binding Path=NA}"  />
                <my:DataGridTextColumn Header="Gender" Binding="{Binding Path=GE}"  />
                <my:DataGridTextColumn Header="Status" Binding="{Binding Path=ST}"  />
                <my:DataGridTextColumn Header="Machine" Binding="{Binding Path=MA}"  />
            </my:DataGrid.Columns>
        </my:DataGrid>
        <Button Height="23" HorizontalAlignment="Left" Margin="36,0,0,53" Name="button1" VerticalAlignment="Bottom" Width="132" Click="button1_Click">student position</Button>
    </Grid>
</Window

>



xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public DataSet ds;
        //public DataSet ds1 = new DataSet();
        public DataTable dt;
        Window _wd;
        public Window1(Window wd)
        {
            //constructor to take value from prev window
            _wd = wd;

            InitializeComponent();
            dt = new DataTable();
            dt.Columns.Add("PO");
            dt.Columns.Add("NA");
            dt.Columns.Add("GE");
            dt.Columns.Add("ST");
            dt.Columns.Add("MA");
            dt.Columns.Add("ImgPath", typeof(BitmapImage));

            DataRow dr = dt.NewRow();
            dr[0] = "1";
            dr[1] = "SAM";
            dr[2] = "Male";
            dr[3] = "Offline";
            dr[4] = "Comp123";
            dr[5] = new BitmapImage(new Uri("/WpfApplication1;component/Images/offline.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "2";
            dr[1] = "RAM";
            dr[2] = "Male";
            dr[3] = "Offline";
            dr[4] = "Comp124";
            dr[5] = new BitmapImage(new Uri("/WpfApplication1;component/Images/offline.png", UriKind.RelativeOrAbsolute));
            dt.Rows.Add(dr);



            dataGrid1.ItemsSource = dt.DefaultView;
            dataGrid1.SelectedValuePath = "NA";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Window2 mw = (Window2)_wd;
            //MessageBox.Show(mw.textBox1.Text);
            DataRow[] result = dt.Select("PO = " + mw.textBox1.Text);
            // AND Sex = 'm'");
            foreach (DataRow row in result)
            {
                //Here I want to change the image of icon to online.png

            }
        }
    }
}
Posted

1 solution

Please replace your code behind (Window1.xaml.cs) with following code.

Note: I assume that you are having online.png image added in your project under Image folder.
Second, see that I haven't modify your XAML file, this code will work with your existing xaml file.
I have added view model classes for giving better control on implementing WPF based applications, do check the button event handler modifications and the way DataViewModel is used.
Please try not creating your dataset in xaml.cs, instead get this from some controller class.


public partial class Window1 : Window
{
    public DataSet ds;
    //public DataSet ds1 = new DataSet();
    public DataTable dt;
    Window _wd;
    public Window1(Window wd)
    {
        //constructor to take value from prev window
        _wd = wd;
        InitializeComponent();
        dt = new DataTable();
        dt.Columns.Add("PO");
        dt.Columns.Add("NA");
        dt.Columns.Add("GE");
        dt.Columns.Add("ST");
        dt.Columns.Add("MA");
        dt.Columns.Add("ImgPath", typeof(BitmapImage));
        DataRow dr = dt.NewRow();
        dr[0] = "1";
        dr[1] = "SAM";
        dr[2] = "Male";
        dr[3] = "Offline";
        dr[4] = "Comp123";
        dr[5] = new BitmapImage(new Uri("/WpfApplication1;component/Images/offline.png", UriKind.RelativeOrAbsolute));
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "2";
        dr[1] = "RAM";
        dr[2] = "Male";
        dr[3] = "Offline";
        dr[4] = "Comp124";
        dr[5] = new BitmapImage(new Uri("/WpfApplication1;component/Images/offline.png", UriKind.RelativeOrAbsolute));
        dt.Rows.Add(dr);

        //dataGrid1.ItemsSource = dt.DefaultView;
        //dataGrid1.SelectedValuePath = "NA";
        dataViewModel = new DataViewModel(dt);
        dataGrid1.ItemsSource = dataViewModel.DataSet;
    }
    DataViewModel dataViewModel;
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        //Window2 mw = (Window2)_wd;
        ////MessageBox.Show(mw.textBox1.Text);
        //DataRow[] result = dt.Select("PO = " + mw.textBox1.Text);
        //// AND Sex = 'm'");
        //foreach (DataRow row in result)
        //{
        //    //Here I want to change the image of icon to online.png
        //}
        dataViewModel.SetOnline();
    }
}

public class DataViewModel : INotifyPropertyChanged
{
    DataTable dt;
    ObservableCollection<DataRowViewModel> dataSet;
    public ObservableCollection<DataRowViewModel> DataSet
    {
        get
        {
            return dataSet;
        }
        set
        {
            dataSet = value;
            OnPropertyChanged("DataSet");
        }

    }
    public DataViewModel(DataTable dt)
    {
        this.dt = dt;
        this.DataSet = new ObservableCollection<DataRowViewModel>();
        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                DataRowViewModel rowViewModel = new DataRowViewModel(dr);
                this.DataSet.Add(rowViewModel);
            }
        }
    }
    #region INotifyPropertyChanged Members
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
    internal void SetOnline()
    {
        if (dataSet != null)
        {
            foreach (DataRowViewModel dr in dataSet)
            {
                dr.SetOnline();
            }
        }
    }
}
public class DataRowViewModel:INotifyPropertyChanged
{
    DataRow dr;
    public string PO
    {
        get
        {
            return dr["PO"].ToString();
        }
        set
        {
            dr["PO"] = value;
            OnPropertyChanged("PO");
        }
    }
    public string NA
    {
        get
        {
            return dr["NA"].ToString();
        }
        set
        {
            dr["NA"] = value;
            OnPropertyChanged("NA");
        }
    }
    public string GE
    {
        get
        {
            return dr["GE"].ToString();
        }
        set
        {
            dr["GE"] = value;
            OnPropertyChanged("GE");
        }
    }
    public string ST
    {
        get
        {
            return dr["ST"].ToString();
        }
        set
        {
            dr["ST"] = value;
            OnPropertyChanged("ST");
        }
    }
    public string MA
    {
        get
        {
            return dr["MA"].ToString();
        }
        set
        {
            dr["MA"] = value;
            OnPropertyChanged("MA");
        }
    }
    public BitmapImage ImgPath
    {
        get
        {
            return dr["ImgPath"] as BitmapImage;
        }
        set
        {
            dr["ImgPath"] = value;
            OnPropertyChanged("ImgPath");
        }
    }
    public DataRowViewModel(DataRow dr)
    {
        this.dr = dr;
    }
    #region INotifyPropertyChanged Members
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
    internal void SetOnline()
    {
        ImgPath = onlineImage;
    }
    private BitmapImage onlineImage = new BitmapImage(new Uri("/WpfApplication1;component/Images/online.png", UriKind.RelativeOrAbsolute));

}
   
v2

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