How to display Bitmap Image in image control on WPF using C# - wpf

I want that when I double click on a row in ListView, it should display the Image corresponding to that row. This row also contains the path of the Image.
I tried the following but it displays the same Image for all rows because I have given the path for a specific Image:
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ImageSource imageSource = new BitmapImage(new Uri(#"C:\northwindimages\king.bmp"));
image1.Source = imageSource;
}
Please suggest something.

They key is to retrieve the row index that was clicked, and get the image URL for that row. Since you say you are clicking on the row, this can be done in a method similar to that below
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRow row = (DataRow)sender; //Get the row that was clicked
string imageURL = row["imageUrl"].ToString();//Get the img URL for that row
ImageSource imageSource = new BitmapImage(new Uri(imageURL));
image1.Source = imageSource;
}
Hope this helps

Suppose that:
The list you are binding to contains Elephant objects and,
You want the image to show Elephant.Picture whenever you double-click an item.
You can set the image from an event handler as follows:
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var viewItem = sender as ListViewItem;
if(viewItem!=null)
{
var elephant = viewItem.DataContext as Elephant;
image1.Source = elephant.Picture;
}
}
Note that it is important to only accept double clicks on the ListViewItem.
The above code assumes that elephant.Picture is of type ImageSource. If it is something else you will have to convert it. For example, if instead Elephant has a string "PicturePath" property, the image1.Source line would change to:
image1.Source = new BitmapImage(new Uri(elephant.PicturePath));

Related

Getting data from detail row

How can i get data from devexress gridcontrol's detail row via double-click.
If i focused on child row gridview's double click event doesn't catch.
i tried this method, but my request is catching data by double click
private void gcOperasyonlar_FocusedViewChanged(object sender, DevExpress.XtraGrid.ViewFocusEventArgs e)
{
if (e.View != null && e.View.IsDetailView)
(e.View.ParentView as GridView).FocusedRowHandle = e.View.SourceRowHandle;
GridView detailView = gcOperasyonlar.FocusedView as GridView;
MessageBox.Show(detailView.GetFocusedRowCellValue("Kalip").ToString());
}
thanks for your help
There is also an easier way:
ColumnView cv = _gridControlxyz.FocusedView as ColumnView;
selectedRow row = cv.GetRow(cv.FocusedRowHandle)
I found this code on the forum, It might be useful as long as your grid is not editable (so that mouse click doesn't activate the editable field).
private void gridView1_DoubleClick(object sender, EventArgs e) {
GridView view = (GridView)sender;
Point pt = view.GridControl.PointToClient(Control.MousePosition);
DoRowDoubleClick(view, pt);
}
private static void DoRowDoubleClick(GridView view, Point pt) {
GridHitInfo info = view.CalcHitInfo(pt);
if(info.InRow || info.InRowCell) {
string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();
MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption));
}
}
http://www.devexpress.com/Support/Center/Question/Details/A2934
Let's say you have two gridviews (I'm guessing you're using gridviews in your grid control): gvMaster and gvDetail.
You should implement event DoubleClick for your gvDetail in order to achieve desired functionality:
private void gvDetail_DoubleClick(object sender, EventArgs e) {
var gv = sender as GridView; // sender is not gvDetail! It's an instance of it. You have as many as there are rows in gvMaster
var row = gv.GetDataRow(e.FocusedRowHandle); // or use gv.GetRow(e.FocusedRowHandle) if your datasource isn't DataSet/DataTable (anything with DataRows in it)
MessageBox.Show(row["Kalip"].ToString());
}

visual studio (2010) combo box with more layers

I would like to ask you something about VS2010 combobox (CB) component...
Is it possible to make something like multilevel (categorised) CB? I mean, can I divide items in CB into categories or somthing like this?
There is similar component in html (tag optgroup) - it's exactly what I need:
multilevel combobox in html
Thanx very much for answer
P.S.: Sorry for my english, I hope I've described it clearly
If you change the DrawMode of the ComboBox to OwnerDrawFixed and you can use the DrawItem event to draw your header and your items. But there is nothing you can do to prevent the user from selecting a header item.
private List<string> groupItems = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
groupItems.Add("Great Bands");
groupItems.Add("Great Bandages");
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.Items.Add("Great Bands");
comboBox1.Items.Add("Led Zeppelin");
comboBox1.Items.Add("Steppenwolf");
comboBox1.Items.Add("Great Bandages");
comboBox1.Items.Add("Band-Aid");
comboBox1.Items.Add("Curad");
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index > -1)
{
string drawText = comboBox1.Items[e.Index].ToString();
if (groupItems.Contains(drawText))
{
using (Font font = new Font(comboBox1.Font, FontStyle.Bold))
e.Graphics.DrawString(drawText, font, Brushes.Black, e.Bounds);
}
else
e.Graphics.DrawString(drawText, comboBox1.Font, Brushes.Black, new Rectangle(16, e.Bounds.Top, e.Bounds.Width - 16, e.Bounds.Height));
e.DrawFocusRectangle();
}
}

WPF Datagrid drag and drop questions

I have a WPF Datagrid and I'm implementing drag and drop functionality.
The datagrid has a list of "files" and the user can drag them and copy the file to the desktop.
This is done like this:
string[] files = new String[myDataGrid.SelectedItems.Count];
int ix = 0;
foreach (object nextSel in myDataGrid.SelectedItems)
{
files[ix] = ((Song)nextSel).FileLocation;
++ix;
}
string dataFormat = DataFormats.FileDrop;
DataObject dataObject = new DataObject(dataFormat, files);
DragDrop.DoDragDrop(this.myDataGrid, dataObject, DragDropEffects.Copy);
I have two questions:
1. When I want to drag multiple items- this is a problem because after I select a couple and start clicking on one to start dragging- only that gets selected and the other items get deselected. I tried the solution that is given here but for some reason it doesn't work.
2. I want to remove the dragged item from the datagrid after it is copied. The problem is that I don't know how to check if the file was copied or whether the user just dragged it on the screen without copying it.
I hope you can help me solve these problems.
Thanks!
I think this i what you are looking for:
add this code to the DataGrid__PreviewMouseLeftButtonDown event handler:
private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.startingPosition = e.GetPosition(null);
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree until get a row or null
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
//if this is a row (item)
if (dep is DataGridRow)
{
//if the pointed item is already selected do not reselect it, so the previous multi-selection will remain
if (songListDB.SelectedItems.Contains((dep as DataGridRow).Item))
{
// now the drag will drag all selected files
e.Handled = true;
}
}
}
and now the draging won't change you selection.
Have a good luck!
I used that article to write my answer
Improved on finding the row.
Also selecting the clicked row when not dragging is added.
This now behaves exactly as other Microsoft selectors (eg Outlook)
public TreeDataGrid()
{
Loaded += TreeDataGrid_Loaded;
LoadingRow += new EventHandler<DataGridRowEventArgs>(TreeDataGrid_LoadingRow);
}
#region MultiSelect Drag
object toSelectItemOnMouseLeftButtonUp;
void TreeDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Row_PreviewMouseLeftButtonDown);
e.Row.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Row_PreviewMouseLeftButtonUp);
}
void Row_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridRow row = (DataGridRow)sender;
toSelectItemOnMouseLeftButtonUp = null; // always clear selecteditem
if (SelectedItems.Contains(row.Item)) //if the pointed item is already selected do not reselect it, so the previous multi-selection will remain
{
e.Handled = true; // this prevents the multiselection from disappearing, BUT datagridcell still gets the event and sets DataGrid's private member _selectionAnchor
toSelectItemOnMouseLeftButtonUp = row.Item; // store our item to select on MouseLeftButtonUp
}
}
void Row_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
DataGridRow row = (DataGridRow)sender;
if (row.Item == toSelectItemOnMouseLeftButtonUp) // check if it's set and concerning the same row
{
if (SelectedItem == toSelectItemOnMouseLeftButtonUp) SelectedItem = null; // if the item is already selected whe need to trigger a change
SelectedItem = toSelectItemOnMouseLeftButtonUp; // this will clear the multi selection, and only select the item we pressed down on
typeof(DataGrid).GetField("_selectionAnchor", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, new DataGridCellInfo(row.Item, ColumnFromDisplayIndex(0))); // we need to set this anchor for when we select by pressing shift key
toSelectItemOnMouseLeftButtonUp = null; // handled
}
}
#endregion

Changing cell in GridView by vlidation

I have a GridView and I want when I change a cell to see if its new value is valid by mine function ValidateValue(string aValue) and if it is valid - to store the new value and old value as a pair in Struct S {string old,new}; How to do this?
Handle the GridView's ValidatingCell event for this purpose. Here is some sample code showing how to obtain new and old edit values:
private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) {
BaseEdit edit = (sender as GridView).ActiveEditor;
object oldValue = edit.OldEditValue;
object newValue = e.Value;
}

Creating WPF examples in LinqPad

Is there any way to sanely instantiate WPF objects in LinqPad? Here's my example (the correct assemblies are added in the query, etc):
var w = new Window();
w.Loaded += (o,e) => {
w.Content = new TextBlock() { Text = "Foo" };
};
w.Show();
However, this dies a horrible death:
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
at System.Windows.Input.TextServicesContext.StopTransitoryExtension()
at System.Windows.Input.TextServicesContext.Uninitialize(Boolean appDomainShutdown)
at System.Windows.Input.TextServicesContext.TextServicesContextShutDownListener.OnShutDown(Object target, Object sender, EventArgs e)
at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)
Any clues on how I can get this to work?
Another way to do it is as follows:
w.ShowDialog();
Dispatcher.CurrentDispatcher.InvokeShutdown(); // Cleanly end WPF session.
More examples:
new Window { Content = "Foo" }.ShowDialog();
new Window { Content = new Button { FontSize = 50, Content = "Foo" } }.ShowDialog();
Dispatcher.CurrentDispatcher.InvokeShutdown(); // Cleanly end WPF session.
You need to run a message loop by calling new Application().Run(w).

Resources