Add a control programmatically to a DocumentContent (AvalonDock) - c#

I have a DocumentContent (AvalonDock) which contains a textEditor. I want to add a toolbar to it at runtime. To do this I need to add a GridDefinition so that the first row has a fixed height (for the toolbar) and the rest of the document content should be filled by the texteditor.
I created a new Grid and added a row definition to it and added the child to it but I don't know how to attach this to the DocumentContent. I am not even sure if it is the right way to add the toolbar. Any suggestions ?
Grid grid = new Grid();
RowDefinition rowDefinition1 = new RowDefinition();
rowDefinition1.Height = new GridLength(32);
grid.RowDefinitions.Insert(0, rowDefinition1);
grid.Children.Insert(0, new IsaDocToolbar());
PS: I forgot to mention that I use AvalonDock 1.3

Why are you doing this in code and not using XAML?
Here is the basic idea/concept using MVVM in XAML
Document content should be a grid with 2 rows
1st row for toolbar
2nd row for text editor
Visibility of the first row can be controlled using binding to a Boolean property and by using BoolToVisibilityConverter.
Hope this helps. I have been doing something similar (not on the document content) for the application. You can checkout my Wide project for a similar concept for toolbars for the window.

Related

WPF datagrid - how to set checkbox in template column without binding?

I have a datagrid, which is populated programatically by data retrieved from a webservice. The last column of the datagrid contains checkboxes, which allow to show each rows value on a map. The column is created with a template column like this:
DataGridTemplateColumn column = new DataGridTemplateColumn();
column.Header = "Show on map";
DataTemplate dt = new DataTemplate();
FrameworkElementFactory checkBoxFactory = new FrameworkElementFactory(typeof(CheckBox));
checkBoxFactory.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(CheckBoxClicked), true);
dt.VisualTree = checkBoxFactory;
column.CellTemplate = dt;
DataGrid.Columns.Add(column);
If the users clicks on one of the checkboxes, the CheckBoxClicked method is invoked and displays the corresponding row values as an icon on the map (as already mentioned). This works very well. However, the user may close the window containing the datagrid, whereas the map stays on the screen, together with the displayed icons.
Now, when the user reopens the window with the datagrid, I'd like to preselect the checkbox, according to the already showing icons. Since the checkboxes do not have a binding, I'm unable to match the icons to them.
How can this be done? Is there a way to programatically preselect them? Or is there still a way to create a binding?
Finally, I've found the solution. Hosch250s comment lead me on the right track (how can I upvote the comment?). The answer can be found here: binding in code.
The easiest way set the checkbox state programmatically is to create this binding:
Binding binding = new Binding("IsChecked");
binding.Mode = BindingMode.OneWay;
binding.Source = this;
checkBoxFactory.SetBinding(CheckBox.IsCheckedProperty, binding);
And implement the property in the current context:
public bool IsChecked {
get {
return doWhateverToFindOutIfChecked();
}
}
The first approach (see my answer with method binding) was simple and strait forward. However, one can not really determine, for which row the binding is called. I've changed the soluntion as followed:
Instead of a DataGridTemplateColumn I'm using a DataGridCheckBoxColumn, now. My data is dynamically stored in rows and bound to the ItemsSource property, using a ListCollectionView. The boolean values, required for the checkbox columns are dynamically inserted into the row values. My datagrid is read only and the selection unit is cell. Settings these properties, I can us the MouseUp event to detect a mouse click on a cell. Inside the handler method, I'll check if the click belonged to a checkbox cell. If so, I change the corresponding boolean value in the row values and call Items.refresh().

How to add vertical scrollbar to combobox programatically

I am using silverlight and my combobox is like this:
ComboBox cb = new ComboBox();
Suppose it already contains Items which are visible only on clicking the combobox.
I want add a vertical scrollbar(or slider) programatically when it shows its items. Is there any inbuilt property for this in silverlight or do I need to use scrollbar or slider for it?
There is an inbuilt property of ScrollViewer.VerticalScrollbarVisibility. Set its value to Visible.
For more read this
You can try changing the dropdownheight property.
cb.DropDownHeight = cb.ItemHeight * 5;
This is to view only 5 items at a time
You can go to Properties:
And set your specified height here.

How to create a new group in a grid view and add items to it?

// Create a new grid view, add content,
GridView gridView1 = new GridView();
gridView1.Items.Add("Item 1");
gridView1.Items.Add("Item 2");
// Add the grid view to a parent container in the visual tree.
stackPanel1.Children.Add(gridView1);
How to create a new group in gridView1 and add items to it?
You can add/remove groups in the grid view and manage it easily by binding a data source.
When you change the data source , grid view will automatically refresh its contents.
To get the basic understanding , analyze the Grid App(XAML) project template provided under Visual C# section of the "New Project" dialog.

bindingnavigator breaks when bound to a datagridview after selection change event

I've used this control before and it was very straight forward. I have a data grid view which is bound to a data source. I add a binding navigator control to the form and set it to use the same binding source as the data grid view. I add a text box, which I use to filter the results that appear in the data grid view, and modify the navigator to remove the add and delete items, so all it contains is the navigation buttons an the text box. Everything appears fine until I select a row in the grid...while the row number in the navigator is correct, there is a big red "X" that spans the entire navigation bar, which spans the entire top of the form. I've included a snapshot here:
I get no warnings or exceptions, and have the debugger set to break on any exception thrown. I feel like I am missing something obvious, but it continues to "work", in that the filter works properly and the row number is always correct. Any advice for helping fix this would be greatly appreciated. The data grid view selection change does modify the contents of the form (other controls on the form) but that all works perfectly.
As far as example code, both the data grid view and the binding navigator use a sql view which was added as a data source.
this.dgvSerial.DataSource = this.vwSerialBindingSource;
this.bindingNavigator1.BindingSource = this.vwSerialBindingSource;
I then set the filter for the binding source to be on the text box that resides in the binding navigator:
vwSerialBindingSource.Filter = string.Format("ProductName LIKE '%{0}%' OR LabelProductName LIKE '%{0}%' OR SerialNumber LIKE '%{0}%'", tb.Text);
I do also add a "search button", with an image which is loaded as an embedded resource.
// setup the search button, the image is
// an embedded resource
bindingNavigator1.ImageList = new ImageList();
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream _imageStream = _assembly.GetManifestResourceStream("SNReprint.search.png");
Image currentImage = Image.FromStream(_imageStream);
ToolStripButton searchButton = new ToolStripButton();
searchButton.Image = currentImage;
searchButton.Text = "Search";
searchButton.Font = new Font(searchButton.Font, FontStyle.Bold);
searchButton.BackColor = Color.Lavender;
// Add the button to the toolstrip
bindingNavigator1.Items.Add(searchButton);
There are a bunch of other controls on the form which are bound to columns in the view which are not affected.
I'm not sure what other code I should provide, so much of it is designer generated, and every other aspect of the application works perfectly. Since this problem doesn't appear until the data grid view selection changed event is raised, I have to assume that I'm doing something in this event handler to cause the binding navigator to render incorrectly.
IF there is anything else specific that I can provide that might shed some light on the problem I am more than happy to post it...

How to show the Small Pointer in WPF DataGrid as in WinForms DataGridView

I am working on WPF and DataGrids, I am really new on this. And I need the WPF DataGrid to show in its row headers the small pointer as in the WindowsForms DataGridView does (it looks like a small triangle pointing to right).
I have tried with this property:
dataGrid1.HeadersVisibility = DataGridHeadersVisibility.All;
But doing as above the dataGrid got some very small headers which seems to be just to select the entire row.
I have done some research about it but I have found nothing.
Does anyone know if this is even possible?
Thank you in advance.
2 things --
1) specify a SortMemberPath -- this will get you the default style 2) style the grid's header template
After some research I have found two links which allow me to do what I was trying to accomplish:
WPF DataGrid Row indicators
or this one:
Changing the style of the selected row of the DataGrid control

Categories