c# WPF Automate GridViewColumnHeader Click event - wpf

Is there a possibility to call the Click-Event of a ListViewColumnHeader programatically?
I try to write an integration test about sortable columns in a ListView with a GridView and want to do something like this:
var list = new ListView();
var grid = new GridView();
var column = new GridViewColumn();
var header = new GridViewColumnHeader();
column.Header = header;
grid.Columns.Add(column);
header.DoClick(); // <-- not possible directly - what can I do
I already tried to achieve the goal by using the GridViewColumnHeaderAutomationPeer but did not succeed.

It is possible to Raise the Click event with the following line of code.
list.RaiseEvent(new RoutedEventArgs(GridViewColumnHeader.ClickEvent, header));

Related

TextBox inside a listview cell

I have a listview that I would like to add a textbox inside each gridview column cell so I can type data into it and then fetch that data.
I'm creating a datatemplate and passing it to a cell template for the GridViewColumn but when I look at the listview I can't add anything to the cell. It doesn't look like the textbox was even created.
GridViewColumn conceptColumn = new GridViewColumn();
conceptColumn.Header = conceptName;
conceptColumn.CellTemplate = this.GetDataTemplate();
this.TestModeler.Columns.Add(conceptColumn);
conceptColumn.DisplayMemberBinding = new Binding(conceptName);
private DataTemplate GetDataTemplate()
{
DataTemplate dt = new DataTemplate(typeof(TextBox));
FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBox));
dt.VisualTree = txtElement;
Binding bind = new Binding();
bind.Path = new PropertyPath("Text");
bind.Mode = BindingMode.TwoWay;
txtElement.SetBinding(TextBox.TextProperty, bind);
txtElement.SetValue(TextBox.TextProperty, "test");
return dt;
}
Please take a look at the ListView Class page at MSDN where you can find a XAML example and plenty of link on how to do various things with a WPF ListView.
Of particular interest to you, please take a look at the How to: Use Templates to Style a ListView That Uses GridView page there which explains what you are trying to do (but in XAML) with examples.
MSDN should always be your first place to look as it is full of information just waiting to be read.

Update the WPF DataGrid Column Header Text via code behind

How can i change the WPF DataGrid Column Header Text via code behind?
i tried the code below but its not working.
this.sampleDataGrid.Columns[0].Header = "New Header";
this.sampleDataGrid.Refresh();
If your DataGridColumn is a Template then you need to change its Template in code.
var template = new DataTemplate();
template.VisualTree = new FrameworkElementFactory(typeof(TextBlock));
template.VisualTree.SetValue(TextBlock.TextProperty, "New Header");
dataGrid.Columns[0].HeaderTemplate = template;

Databinding bindinglist to combobox and removing items

I am trying to use windows forms databinding to hook up a combobox to a ViewModel class.
var items = new BindingList<Person>();
comboBox.DataSource = items;
comboBox.DisplayMember = "Name";
All works ok except when I remove items from the list. For example if I remove the currently selected item (selected in the combobox) the selectedIndexChanged and SelectedValueChanged events of the combobox don't fire.
Found an answer. I had to use a BindingSource as middleman
var bindingsSource = new BindingSource();
bindingsSource.DataSource = new BindingList<Person>();
comboBox1.DataSource = bindingsSource;
comboBox1.DisplayMember = "Name";
This way I do get value changed events and even more than one when I deleted something.

Silverlight 4: How to access control created from codebehind

I'm trying to create a method to draw a Line(path) between two UserControls. I found a post by someone that gave me a general pointer on how to do this, I implemented the code succesfully and started adapting it to my needs.
I am having a problem with accessing the user control:
Button b2 = new Button();
var transform2 = b2.TransformToVisual(b2.Parent as UIElement);
Works as it should, but my buttons get created dynamicaly through a method so I can't access them as "b2".
I tried the following:
var transfrom3 = canvas1.Children[0].TransformToVisual(canvas1.Children[0].Parent as UIElement);
but accessing it like that gives me an error on .Parent.
If have also tried:
var p1 = this.FindName(ps.ProcessID.ToString());
var p2 = this.FindName(ps.PreID.ToString());
////get geo data from both controls
var transform1 = p1.TransformToVisual(p1.Parent as UIElement);
var transform2 = p2.TransformToVisual(p2.Parent as UIElement);
Can anyone tell me how i can access these UserControls?
This line ought to work:-
var transfrom3 = canvas1.Children[0].TransformToVisual(canvas1.Children[0].Parent as FrameworkElement);
as you have discovered UIElement doesn't have Parent, its the FrameworkElement that adds the Parent property.
However it would be better to access the control via its name (I'll assume you are assigning a Name to the dynamically added controls).
This:-
var p1 = this.FindName(ps.ProcessID.ToString());
ought to work assuming this is UserControl and the FrameworkElement in question was given the matching name in code:-
canvas1.Children.Add(new Button() {Name = ps.ProcessID.ToString(), Content="Hello"});
Hmm I did not see that you changed UIElement to FrameworkElement, this setup works for me now:
var control1 = this.FindName(ps.ProcessID.ToString()) as FrameworkElement;
var control2 = this.FindName(ps.PreID.ToString()) as FrameworkElement;
var transform1 = control1.TransformToVisual(control1.Parent as UIElement);
var transform2 = control2.TransformToVisual(control2.Parent as UIElement);
If I add the control to a var as a FrameworkElement, then I can use the .Parent on the control again.
Thanks for your answer Anthony!

WPF Tooltip binding not updating

Good afternoon all,
I have to work with a legacy Winforms application but I'd like to start migrating it to WPF. It doesn't have a tooltip control now so I'd like to use a WPF tooltip object.
I create a single global instance of a tooltip object. I've bound the controls within it and my application sets the datacontext of the tooltip. I can manually show and hide the tooltip just fine. The first time I hover over an object it picks up the bound data perfectly and works great. When I move over another control the tooltip datacontext is changed but the displayed data is never reloaded.
I tried implementing a property changed event and use the INotifyPropertyChanged interface in the object I bind to. It appears the wpf tooltip is not listening to the event.
I tried setting the binding mode to Oneway (it's a display only tooltip).
The tooltip is created programmatically:
// build the tooltip window.
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Width = 50;
img.Height = 60;
// bind the image
System.Windows.Data.Binding imageBinding = new System.Windows.Data.Binding("PatientImage.Data");
imageBinding.Mode = System.Windows.Data.BindingMode.OneWay;
imageBinding.Source = bed;
img.SetBinding(System.Windows.Controls.Image.SourceProperty, imageBinding);
// wrap image in a border
System.Windows.Controls.Border brdr = new System.Windows.Controls.Border();
brdr.BorderBrush = System.Windows.Media.Brushes.Blue;
brdr.Margin = new System.Windows.Thickness(6);
brdr.Child = img;
System.Windows.Controls.WrapPanel wp = new System.Windows.Controls.WrapPanel();
System.Windows.Controls.TextBlock tb = new System.Windows.Controls.TextBlock();
tb.Background = System.Windows.Media.Brushes.LightBlue;
tb.Foreground = System.Windows.Media.Brushes.Blue;
// bind the text block
System.Windows.Data.Binding textBlockBinding = new System.Windows.Data.Binding("TooltipText");
textBlockBinding.Mode = System.Windows.Data.BindingMode.OneWay;
textBlockBinding.Source = bed;
tb.SetBinding(System.Windows.Controls.TextBlock.TextProperty, textBlockBinding);
wp.Children.Add(brdr);
wp.Children.Add(tb);
bedTooltip = new System.Windows.Controls.ToolTip();
bedTooltip.Content = wp;
Any idea why this doesn't work? Maybe I need to use a tooltip object for each control instead of a single global one as a workaround?
The bindings specify a Source, because of that they no longer "care" about the DataContext and hence the bindings do not update if anything other than the property itself on the source-object changes.

Resources