How do you access Expander content dynamically? - wpf

I have multiple dynamic generated Expanders having DataGrid as Content, and CheckBoxes in DataGrid. I got the DataGrid with the following code below but don't know how to get the Expander.
private void ChkBoxDamage_Checked(object sender, RoutedEventArgs e)
{
CheckBox b = (CheckBox)e.Source;
DataGridRow row = b.TemplatedParent.TryFindParent<DataGridRow>();
row.Background = System.Windows.Media.Brushes.Red;
DataGrid dataGrid = b.TemplatedParent.TryFindParent<DataGrid>();
Expander Gridexpanderr = dataGrid.TemplatedParent.TryFindParent<Expander>();
Gridexpanderr.Background = new SolidColorBrush(Colors.Red);
}
The above code won't change expander Background Color.

Related

WPF Get selected item from DataGrid and open a new Window

I have a DataGrid, and in DataGrid are listed users from Database.
I want that , when i click on a member in DataGrid, open a new Window , and show me the members data(Username, Postal code, ... etc what is in the database).
How to open a new window , when i Click on a row?
Solved:
private void dg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var windowToOpen = new UsersWindow();
windowToOpen.Show();
}
XAML:
<DataGrid x:Name="NameGrid" MouseLeftButtonDown="dg_MouseLeftButtonDown"/>
Are you using MVVM or doing codebehind?
In any case you should be able to listen on selectedItemChanged and handle it your way
EDIT
Ok. Never tried this by myself..
DataGrid has an SelectedItem property. This gives you the first Row in your selection (If you have Multiselect).
So basically you can do in your Codebehind:
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedUser = this.theNameOfYourGrid.SelectedItem;
var windowToOpen = new Window();
/*
* Do what you want with your window
*
*
*/
windowToOpen.Show();
}
and in your XAML
<DataGrid SelectionChanged="Selector_OnSelectionChanged" x:Name="theNameOfYourGrid">
</DataGrid>
that should it be

WPF drag item from listbox and drop in textbox

I'm trying to drag an item from a listbox to a textbox in WPF.
I can't get any code that will let me do this.
Thanks
Use selection change event of listbox
private void listBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem i = (ListBoxItem)listBox2.Items[listBox2.SelectedIndex];
s = i.Content.ToString();
DragDrop.DoDragDrop(listBox2, i.Content.ToString(), DragDropEffects.All);
}

How to force DataGrid to rebuild VisualTree for its columns

I have WPF form with DataGrid. New columns can be added to the datagrid manually by user via button. This is the code to add new column:
private void ColumnAdornerAddButton_MouseDown(object sender, MouseButtonEventArgs e)
{
DataGridTextAdornerColumn column = new DataGridTextAdornerColumn();
column.Header = "New column";
column.HeaderStyle = (Style)FindResource("columnHeader");
column.AdornerTemplate = (DataTemplate)FindResource("columnAdorner");
Binding binding = new Binding("Data");
binding.Mode = BindingMode.TwoWay;
column.Binding = binding;
grid.Columns.Insert(grid.Columns.Count - 1, column);
//Add adorner
DataGridColumnHeader header = GetColumnHeaderFromColumn(column);
AddAdorner(header, column.AdornerTemplate, column.IsReadOnly);
}
private DataGridColumnHeader GetColumnHeaderFromColumn(DataGridColumn column)
{
// dataGrid is the name of your DataGrid. In this case Name="dataGrid"
List<DataGridColumnHeader> columnHeaders = GetVisualChildCollection<DataGridColumnHeader>(grid);
foreach (DataGridColumnHeader columnHeader in columnHeaders)
{
if (columnHeader.Column == column)
{
return columnHeader;
}
}
return null;
}
The problem is that after I have added the column to the grid its header is not yet generated and it is not present in visual tree. Thus I cant get header for the new column and apply adorner to it.
I have tried to call ApplyTemplate recursively on visual tree of the grid without any luck.
Is there any way to force grid to generate DataGridColumnHeader for the new column in code?
Thank you in advance.
Hi after adding columns to datagrid call the method UpdateLayOut() of DataGrid.
datagrid.UpdateLayout();
I hope this will help.
I just want to enhance the solution,
The method
datagrid.Items.Refresh();
will helps to recreate the view (Datagrid).
thus you can see the updated value in datagrid

How to capture cell content in a Silverlight datagrid?

I would like to capture the value of a cell in a datagrid when the user click on that cell. I dont want to edit the cell, just click on it and capture the value so i can do somthing with it in the viewmodel. Help appreciated.
if your DataGrid is
<Controls:DataGrid Name="versionInfo" PreparingCellForEdit="grid_ForEdit" >
the handler is
private void grid_ForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
TextBox box = e.EditingElement as TextBox;
// box.Text contains the text in that cell
}

ListView Expander issue in wpf

I am using a checkbox on expander. I want, when I check that checkbox ,it will select all rows of list view those are coming under that expander.
set the listview's selectionmode to Multiple' or 'Extended and
on the CheckBox_Checked event you can write the code to select the rows of listview
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
listview1.SelectAll();
}
Try this inside your checkbox_click events handler:
listView1.BeginUpdate();
foreach (ListViewItem i in listView1.Items)
{
i.Selected = true;
}
listView1.EndUpdate();
Which gives you a bit more flexibility.

Resources