How to combine two displays in WPF? - wpf

I have two displays. One resides inside a UserControl and the other one is a Rectangle. I am passing the Rectangle as a parameter to a UserControl method. Now inside my ViewModel Method, I want to combine this Rectangle with the UserControl display. How can I achieve this combined display?
ViewModel Code -
private UserControl CreateCombination(Rectangle rect)
{
//This UserControl contains one its display (basically a combination of few controls)
CombinedUC combination = new CombinedUC();
// I want to write some code here so that I can place combination object just above my rectangle (rect).
return combination;
}

Related

How do I make user controls for both ListView and ListViewItem work with each other?

I have all the styling, triggers, etc. down for ListView and ListViewItem, and I want to turn them into user controls. How do I make sure that these two "match up" with each other, so that MyListView accepts MyListViewItems as content? Also, considering that I must end the ListView tag by the end of the user control XAML file, I am not sure how I would add items to it.
If you want them to be reusable with different data sets, especially through binding, you should stay away from UserControls and just make custom controls derived from the original types. In that case you create a standalone MyListView.cs and MyListViewItem.cs and all of the XAML for the controls goes into default Styles (usually also containing a ControlTemplate) in Themes/Generic.xaml. You can see an example of this setup by just adding a WPF Custom Control to your WPF project from Add New Item.
Once you've created the .cs files for your custom controls you just need to override a few methods from the base ItemsControl to use MyListViewItem as the item container control. The ListView would end up like this:
public class MyListView : ListView
{
static MyListView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyListView), new FrameworkPropertyMetadata(typeof(MyListView)));
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MyListViewItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is MyListViewItem;
}
}
You can now use your custom MyListView exactly as you would a normal ListView, including binding to ItemsSource.
Inheritance should take care of that for you. In other words, if you have two user controls, the first one with a basic element of ListView (not UserControl) and the other of ListViewItem (again, not UserControl), and you make sure they extend ListView and ListViewItem respectively in the .cs code, the following should work equally:
ListView lv = new ListView();
lv.Items.Add(new ListViewItem());
or
MyListView mlv = new MyListView();
mlv.Items.Add(new myListViewItem()); //If your myListView extends ListView, and myListViewItem extends ListViewItem in your user control files, of course
In case you are looking for a XAML solution, you should import your namespace at the top
xmlns:myControls="WhateverYourNamespaceAndAssemblyAre"
and on you page/window/whatever
<myControls:myListView>
<myControls:myListViewItem/>
<myControls:myListViewItem/>
</myControls:myListView>

WPF: Setting Keyboard Focus in a User Control? (Problems with KeyBinding)

I have an app that has a main window that contains a bunch of stuff. From time to time the user will do something in response to which I want to display something else entirely in the main window, temporarily hiding what is there.
I'm doing this by making the outermost element in the main window a Grid with no rows or columns defined. Every element in the grid, then will completely fill the one single cell in the grid, drawing on top of the others.
My regular bunch stuff, then, is in the first element of the grid, and my temporary something else is a UserControl as the second element of the grid, that is normally set Visibility=Collapsed.
Except for the KeyBinding everything works fine. When the appropriate command is triggered in the regular bunch of stuff, the visibility on the UserControl is set to Visible, and it covers the regular bunch of stuff completely. When the user clicks on the close button on the UserControl, it's visibility is set to Collapsed, again, and it disappears and the regular bunch of stuff is revealed.
My problem is with KeyBindings. I have a few defined on the UserControl - that should not be defined on the main window - and they don't work. Or rather, they work fine once I click inside the UserControl, but they don't work until I do.
I need them to work as soon as the UserControl is made visible, without requiring the user to click or tab into the UserControl proper.
My guess is that this has something to do with keyboard focus - but I've been unable to find a way to set the focus on the UserControl. Here's the thing - the only element within the UserControl is a tab control, all of the tabs of which are dynamically constructed via templates. There are no elements known at compile time that I can reference explicitly and pass to KeyBoard.Focus().
So, am I right in thinking that it's lack of focus that is causing the problem? And if so, how can I set focus to an element in a TabControl, when I don't even know how many tabs there are, let alone which is the selected one?
I wanted this control to have the focus, when it became visible. So in the constructor, I set up a handler on IsVisibleChanged.
public MyControl
{
...
this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(MyControl_IsVisibileChanged);
}
void MyControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (!(bool(e.NewValue)
return;
this.Focusable = true;
Keyboard.Focus(this);
}
I could have set Focusable in the xaml, but I prefer it in the code-behind, so that all of the relevant code is in one place.
For me this worked in the code-behind:
using System.Windows.Input;
namespace MyApplication.Views.Dialogs
{
public partial class MyControl
{
public MyControl()
{
InitializeComponent();
Loaded += (sender, args) =>
{
MyButton.Focus();
Keyboard.Focus(MyButton);
};
}
}
}

how to Show MDIChild Form on Top of the MDIParent's Controls

I have a MDI-Parent Form with many ChildForms, when I want to add a control on my Parent form, Child form appears under the control, For example I want to add a groupbox and a PictureBox on MDIParent Form, but when I call the Child Form it appears Under these controls.
frmChildForm1.TopMost=true doesn't works either.
I have attached a photo for more description.
What can I do?!
but I want to have an Image as Background
That's possible, you can set the BackgroundImage property of the MDI client control. The only obstacle is that you cannot directly get a reference to that control. You have to find it back by iterating the form's Controls collection. Like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
foreach (Control ctl in this.Controls) {
if (ctl is MdiClient) {
ctl.BackgroundImage = Properties.Resources.Lighthouse;
break;
}
}
}
}
Where Lighthouse was a sample image I added as a resource. Change it to use your own. Another common technique is to subscribe the Paint event for that control and draw whatever you want. A gradient is a common choice.

Creating a customizable grid overlay

Just looking for some input as to what control I should go with or a broad approach. I am going to load up a png in the program I am writing. Then I could specify that I want 32x32 lines split over the picture (I'm not breaking the picture up, just specifying a grid to be on top). So, obviously I am going to need something which I can select multiples of these "cells" (which the grid or whatever broke into) and easily identify which the user is selecting. Does the grid do this or is it something more like creating guidelines and then creating some rectangles or something?
You would use an ItemsControl or derived class such as the Selector with the ItemsPanel property set to a Grid. In the ItemsContainerStyle property would set the Style for a ContentControl. The ContentControl is a the of object that will be generated for each item in the list that your ItemsControl will be bound against using the ItemsSource property. In that style you will setup a ControlTemplate for the ContentControlto soemthing that includes a Border or Rectangle or similar to get the grid lines. The root Control in your ControlTempalte will have the Grid.Row and Grid.Column properties bound against the .Row and .Column properties of your dataitems wich will be the DataContext.
Finally you bind the ItemsControl agains an ObservableCollection of these DataItems.
<ItemsControl ItemsSource={Binding MyDataItems} ....
Your DataItem would look something like this:
public class DataItem : INotifyPropertyChanged
{
public int Row
{
get { // return field }
set { // raise the PropertyChanged event here }
}
public int Column
{
get { // return field }
set { // raise the PropertyChanged event here }
}
}

In WPF, is it possible to have a combo box control, that looks like a RadioButton?

I have a whole bunch of code that is dependent on the ComboBox type, and I want to be able to use it on a new UI, but the UI that I want to use it for needs to look like a RadioButton for usability reasons. Is it possible to have a ComboBox control look like a set of RadioButtons?
My suggestion would be to use an ItemsControl with a DataTemplate that would render RadioButtons. To the ItemsControl you'd bind the same thing you're binding to the ComboBox.
One caveat is that you need to set the GroupName of the radio buttons to something that would be the same to the group, so they can be mutually exclusive. Otherwise, if you don't do anything, you'll be able to select more than one RadioButton simultaneously.
You could build a new UserControl that has many of the same methods that the ComboBox class does, but adapt it so that it creates multiple radio boxes instead.
Your question is kinda vague though.
IE create an Items collection on your user control, and when something is added, draw a radio box and resize your control, instead of what a combo box does and just adds a string to the list.
Then all you have to do is find and replace all your references to ComboBox with RadioIFiedComboBox.
Heres some comparison:
ComboBox cb = new ComboBox();
cb.Items.Add("blah");
or
RadioIFiedComboBox cb = new RadioIFiedComboBox();
cb.Items.Add("blah");
and
public class RadioIFiedComboBox : UserControl {
public ObservableCollection<object> Items = new ObservableCollection<object>();
public RadioIFiedComboBox() {
Items.CollectionChanged += new NotifyCollectionChangedEventHandler(YourCollectionChanged);
}
private void YourCollectionChanged(){
//do something here to redraw your controls
}
}
The code above is just an example, you'd have to create all the methods you use in the ComboBox class and create similar functionality.

Resources