Why DataBinding is not propagating to UserControl - c#

This morning I asked a question here and doing a simple working sample gave me a different behavior than expected.
Full working sample at GitHub. Main partial code below.
In this present case, the command is never propagated to any UserControl, either if the UserControl is use directly as a child of the Window. It also not work if the UserControl is used as a DataTemplate for a ListBox ItemTemplate.
I also include a hack button to fix the problem where the Command reach the UserControls. The hack come from StackOverflow.
But using the hack does not explain why UserControl does not receive the Command (without it) and using this hack also break the first rule of good coding: "Hi cohesion and Low coupling". The hack should be used in the the window code in order for it to manage the Command in the UserControl, my thought is that it should happen by default.
Why the command is not propagating by default to the UserControl and what should I do to propagate the command to the UserControl in a clean way?
Note: Using only one CommandBinding (removing one or the other) in the UserControl does not fix the problem .
Partial code:
<Window x:Class="CommandRoutingIntoItemTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CommandRoutingIntoItemTemplate"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<local:UserControlTest></local:UserControlTest>
<ListBox Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Aqua" BorderThickness="2">
<local:UserControlTest></local:UserControlTest>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<system:String>1</system:String>
<system:String>2</system:String>
</ListBox.Items>
</ListBox>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button Command="local:Commands.CommandTest">Put focus on TestBlock and click here to see if command occurs</Button>
<Button Click="AddHack">Hack</Button>
</StackPanel>
</Grid>
</Window>
UserControl:
<UserControl x:Class="CommandRoutingIntoItemTemplate.UserControlTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CommandRoutingIntoItemTemplate"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.CommandBindings>
<CommandBinding Command="local:Commands.CommandTest" CanExecute="CommandTestCanExecuteUserControl" Executed="CommandTestExecuteUserControl"></CommandBinding>
</UserControl.CommandBindings>
<Grid>
<TextBox Text="UserControlTest">
<TextBox.CommandBindings>
<CommandBinding Command="local:Commands.CommandTest" CanExecute="CommandTestCanExecuteTextBox" Executed="CommandTestExecuteTextBox"></CommandBinding>
</TextBox.CommandBindings>
</TextBox>
</Grid>
</UserControl>

The reason that you are not getting commands invoked on your user control is that your buttons are not in separate focus scope. For WPF to pick up focused element for command target correctly, it needs to be in separate focus scope from command invoking control.
Framework will just traverse up visual tree from button looking for command bindings in their focus scope (in your case it won't find any). When framework does not find any command bindings in current focus scope, only then it looks into parent focus scope for focused element (In your case, buttons are in Window focus scope which has no parent scope so the search will end there).
Simply setting FocusManager.IsFocusScope="True" on your StackPanel will fix the issue.
You could also specify CommandTarget property on your buttons to point to your user control and not rely on focus.

Related

Overlapping Transparent Views in a Prism Region

I was wondering if there is a way to show two overlapping transparent views in one named Region? My example below shows two overlapped transparent views in their own separate named region container in the first grid row. In the second grid row we have one named region "RegionC". The first registered view is the one that is displayed ("ViewA"). Am I correct that if we have multiple views registered to a region then we can only show one view at a time? Is there a way to show two overlapped views in one named Region? Or is it standard practice to add another content control to support multiple views shown? One reason why I want to do this is so I can better separate my XAML code in separate views and inject them into one region container as needed.
ShellWindow.xaml
<Window x:Class="PrismDemo.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="150" Width="325" >
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" prism:RegionManager.RegionName="RegionA" />
<ContentControl Grid.Row="0" prism:RegionManager.RegionName="RegionB" />
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="RegionC" />
</Grid>
ShellWindow.xaml.cs
using Prism.Regions;
using System.Windows;
namespace PrismDemo.Views
{
public partial class ShellWindow : Window
{
public ShellWindow(IRegionManager regionManager)
{
InitializeComponent();
regionManager.RegisterViewWithRegion("RegionA", typeof(ViewA));
regionManager.RegisterViewWithRegion("RegionB", typeof(ViewB));
regionManager.RegisterViewWithRegion("RegionC", typeof(ViewA));
regionManager.RegisterViewWithRegion("RegionC", typeof(ViewB));
}
}
}
ViewA.xaml
<UserControl x:Class="PrismDemo.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PrismDemo.Views"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300">
<TextBlock Text="ViewA" FontSize="20" />
ViewB.xaml
<UserControl x:Class="PrismDemo.Views.ViewB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PrismDemo.Views"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300">
<TextBlock Text="ViewB" FontSize="30" />
You should use ItemsControl instead of ContentConrol for your region RegionC and set its ItemsPanel to Grid.
Here is the XAML:
<ItemsControl Grid.Row="1" prism:RegionManager.RegionName="RegionC">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Am I correct that if we have multiple views registered to a region then we can only show one view at a time?
For regions in a ContentControl, yes, you are correct, because it can only have one Content at a time. But basically any control can host a region (provided you create an associated regionAdapter), for example, an ItemsControl or a TabControl can have multiple views inside them at the same time.
Is there a way to show two overlapped views in one named Region?
Yes, if you provide a RegionAdapter for a Grid, for example.
Or is it standard practice to add another content control to support multiple views shown?
Yes and no. Standard for multiple views in multiple regions, not standard for multiple views in the same region.

Make region fullscreen in PRISM application by stretching user control

I am creating application using PRISM library. Currently i had only one region. Inside this region i put a user control with my view (as you can see below). Is just a some menu with property grid and a list to display data.
My user control:
By default my main window is full screen. When I run my application there are some remaking blank space below my user control.
Question:
How i can stretch my user control to fill all available space? As on picture below
What i've done:
- My user control container currently is DockPanel i tried put it to grid and other controls
- Delete user control sizes (width and height)
- Use viewbox inside shell.xaml (it destroys everything)
- Put user control to different cotainers inside shell.xaml
Currently shell.xaml is my "main window" I has only window tags with my user control.
I find similar questions on stack overflow but nothing helped me.
--- UPDATE ---
This is example of my user control code:
<UserControl x:Class="NewPrj.View.FullScreenTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d">
<DockPanel Background="Blue" LastChildFill="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Menu DockPanel.Dock="Top">
<MenuItem Header="_New"/>
<MenuItem Header="_Something"/>
</Menu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="350" />
</Grid.ColumnDefinitions>
<xctk:PropertyGrid Grid.Column = "1"
>
</xctk:PropertyGrid>
</Grid>
</DockPanel>
This is my shell.xaml window
<Window x:Class="NewPrj.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NewPrj"
xmlns:prism="http://www.codeplex.com/prism"
mc:Ignorable="d"
WindowState="Maximized"
Title="New Prj" MinHeight="600" MinWidth="800">
<Grid>
<ItemsControl Name="MainRegion" prism:RegionManager.RegionName="MainRegion" />
</Grid>
And this is actual result:
As I said before, there are many related topics but it does not work.
Reason:
This was not fully stretched because i use ItemsControl which use stack panel as default data template
Solution:
Change data template of ItemsControl or use User Control instead
<UserControl Name="MainRegion" prism:RegionManager.RegionName="MainRegion"/>
I found answer here

Unable to edit TextBox in a UserControl WPF

I am working on a WPF application in which a user control is loaded over main window at run time.
I have a TextBox in the UserControl which is supposed to be a editable TextBox. But I am not able to edit it using keyboard input. Whereas, i am able to set the text pragmatically.
I have refered MSDN forum regarding the issue. Still i am not able to get through. My code is similar to one in the link above.
Please share your thoughts to get this done.
Thanks in advance.
Edit:
xaml code:
<UserControl x:Class="UserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Loaded="UserControl_Loaded"
FocusManager.IsFocusScope="True"
FocusManager.FocusedElement="{Binding ElementName=TestTxt}">
<Grid FocusManager.IsFocusScope="True">
<StackPanel Orientation="Horizontal" FocusManager.IsFocusScope="True">
<TextBox x:Name="TestTxt" IsReadOnly="False" IsEnabled="True" FocusManager.IsFocusScope="True"/>
</StackPanel>
</Grid>
In UserControl_Loaded add TestTxt.Focus();

The Elementhost and the Popup

I got a problem with the Windows.Forms.ElementHost. Within the host I placed a WPF UserControl, in which I got a Popup.
Some Code:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="380">
<Grid>
<Popup x:Name="TestPopup"
IsOpen="{Binding PopupVisible,UpdateSourceTrigger=PropertyChanged}"
Placement="Center"
StaysOpen="False" Width="200" Height="50">
<Grid>
<TextBox/>
</Grid>
</Popup>
<Button Click="Button_Click" />
</Grid>
</UserControl>
So in this example all I got is a ElementHost, a UserControl and a Popup (and a ViewModel in which the IsOpen variable PopUpVisible for the popup is implemented).
Now I got two Buttons... one in my UserControl and one in my WindowsForm with the ElementHost.
Each of the two buttons sets the IsOpen variable PopUpVisible to true. So if I push the button´s the same popup is shown. Until this point everything works fantastic.
Now I got a textbox in my popup... I click in this textbox and begin tipping some random stuff... If I push the button in my wpf usercontrol this works! But if I push the button in my WindowsForm things start to get weird! I got focus and everything on my textbox but the textbox won´t get any of my keyboard tipping. I checked and doublechecked it... I definitely got my "Keyboard Focus" on the textbox!
A bit stuck here... someone has an idea?
Kind Regards.

WPF menu closes on mouseup when window open from excel add-in project

I have a problem when creating a menu in WPF. What happens is that it closes automatically when you stop pressing the mouse button. I want it to behave as regular menu's where you can click and the subitems will stay up but I can't find anyway to get this done.
The code looks like this:
<Window x:Class="ExcelAddIn.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DockPanel>
<Menu Width="Auto" IsMainMenu="True" >
<MenuItem Header="Item">
<MenuItem Header="SubItem" />
</MenuItem>
</Menu>
</DockPanel>
</Grid>
</Window>
I'm wondering if it has anything to do with logical focus maybe? I saw something about it might being a bug in .NET framwork? Any ideas?
Thanks in advance
I didn't think it made any difference at first but obviously it does. When running the code in a standalone WPF application it works, however when I try to open the WPF window from a Excel-addin project I get this problem..
Ok! I solved the problem. Turns out it was a focus problem after all.
When the excel addin executed the WPF window the excel window was still in focus. So on every mouseup the focus would jump back from WPF to excel.
All I had to do was change the execution from this:
MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.Show();
to this:
MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.ShowDialog();
Thanks for help anyway guys!
I adjusted your code a tiny bit (DockPanel.Dock="Top" and an extra grid to fill the rest of the dock panel). It works fine and the menu stays open. Does it work for you?:
<Window x:Class="WpfApplication2.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<Grid>
<DockPanel>
<Menu Width="Auto"
IsMainMenu="True" DockPanel.Dock="Top">
<MenuItem Header="Item">
<MenuItem Header="SubItem" />
</MenuItem>
</Menu>
<Grid />
</DockPanel>
</Grid>
</Window>

Categories

Resources