WPF UserControl: Open another UserControl (Popup) when click on UserControl object - wpf

I have built a button wrapper around the User Control (FFU), so the object is clickable trough the main window. When the FFU object is clicked, I want to open another User Control: Popup FFU to be opened.
Main window XAML
<vw:BaseWindow
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="http://inosoft.com/visiwin7"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:UserControls="clr-namespace:HMI.Windows.UserControls" x:Class="HMI.MainWindow"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStyle="None" WindowState="Normal" WindowStartupLocation="CenterScreen"
Width="1024" Height="768">
<Grid x:Name="LayoutRoot" UseLayoutRounding="True">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<vw:Region x:Name="MainRegion" StartView="MainView1" DesignTimeView="MainView1" Grid.Row="0" Margin="0,96,0,4" Grid.RowSpan="2" />
<vw:Region x:Name="HeaderRegion" StartView="HeaderView" DesignTimeView="HeaderView" Grid.Row="0" />
<vw:Region x:Name="FooterRegion" StartView="FooterView" DesignTimeView="FooterView" Grid.Row="2" />
<UserControls:UC_FFU x:Name="FFU_1" HorizontalAlignment="Left" Height="28" Margin="305,85,0,0" Grid.Row="1" VerticalAlignment="Top" Width="44"/>
<UserControls:UC_FFU x:Name="FFU_2" HorizontalAlignment="Left" Height="29" Margin="358,85,0,0" Grid.Row="1" VerticalAlignment="Top" Width="44"/>
<UserControls:UC_FFU x:Name="FFU_3" HorizontalAlignment="Left" Height="29" Margin="410,85,0,0" Grid.Row="1" VerticalAlignment="Top" Width="44" StateBrush="{vw:VariableBinding VariableName=MCS1.Cleanroom.SIM_Cleanroom.SIM_FFUControl1.Observers.oStatus, Converter={StaticResource ValueToStateBrushConverter}, States={StaticResource BrushListStatus}, StateMode=Value}" PopupFFUInstance="{Binding MyDataContextPopupFFUProperty}"/>
</Grid>
</vw:BaseWindow>
UserControl code XAML
<UserControl x:Class="HMI.Windows.UserControls.UC_FFU"
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:HMI.Windows.UserControls"
mc:Ignorable="d"
x:Name="_this" d:DesignWidth="42" Height="27.544">
<Button x:Name="Btn" IsHitTestVisible="{Binding ElementName=Popup, Path=IsOpen, Mode=OneWay}" Click="Btn_Click">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="19*"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="{Binding ElementName=_this, Path=StateBrush}" Width="40" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,0,0" Grid.ColumnSpan="6"/>
<Line Stroke="#FF000000" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,0,0" Width="0" Y1="0" Y2="24" X1 ="-.5" X2="-.5"/>
<Line Stroke="#FF000000" Width="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin=".5,25,-2,0" Width="43" Y1="0" Y2="0.114682539682633" X2="40.5" Grid.ColumnSpan="6"/>
<Line Stroke="#FF000000" Height="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,-2,0" Width="43" Y1="0" Y2="0.114682539682633" X2="39.5" Grid.ColumnSpan="6"/>
</Grid>
<Button.Template>
<ControlTemplate x:Name="BlankButtonTemplate" TargetType="{x:Type ButtonBase}">
<Border Background="#00000000">
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</UserControl>
UserControl FFU backhand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HMI.Windows.UserControls
{
/// <summary>
/// Interaction logic for UC_FFU.xaml
/// </summary>
public partial class UC_FFU : UserControl
{
//Create PopupFFUProperty
public static readonly DependencyProperty PopupFFUProperty =
DependencyProperty.Register("PopupFFUInstance", typeof(int), typeof(UC_FFU));
public int PopupFFUInstance
{
get { return (int)GetValue(PopupFFUProperty); }
set { SetValue(PopupFFUProperty, value); }
}
public UC_FFU()
{
InitializeComponent();
}
//Added the Click Event
public event RoutedEventHandler Click
{
add { Btn.AddHandler(ButtonBase.ClickEvent, value); }
remove { Btn.AddHandler(ButtonBase.ClickEvent, value); }
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
// Something to open the UserControl FFU Popup....
}
}
}
Simple Pop up example FFU XAML
<UserControl x:Class="HMI.Windows.UserControls.PopupControl.Popup_FFU"
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:HMI.Windows.UserControls.PopupControl"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="50">
<Popup IsOpen="{Binding IsChecked, ElementName=button}" StaysOpen="False">
<Border Background="LightYellow">
<TextBlock>I'm the popup</TextBlock>
</Border>
</Popup>
</UserControl>
How can this be achieved?
I have tried to open a new window like this: however it should be a popup. Not a dialog window.
Window window = new Window
{
Title = "My User Control Dialog",
Content = new PopupControl.Popup_FFU()
};
window.Show();

This is what I would change in your code.
UC_FFU.xaml: Put this before your closing </Grid>
<Popup x:Name="ButtonPopup" StaysOpen="False">
<UserControls:Popup_FFU />
</Popup>
UC_FFU.xaml.cs:
private void Btn_Click(object sender, RoutedEventArgs e)
{
ButtonPopup.IsOpen = true;
}
Popup_FFU.xaml:
<UserControl x:Class="HMI.Windows.UserControls.PopupControl.Popup_FFU"
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:HMI.Windows.UserControls.PopupControl"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="50">
<Border Background="LightYellow">
<TextBlock>I'm the popup</TextBlock>
</Border>
</UserControl>

Related

How do I get the outline of a UI element?

Basically WPF elements are rectangular in shape.
And it is very easy to get their outline through their Width and Height.
But in the general case, the contour of an element can be any line, including consisting of several contours that do not intersect with each other.
I expected that the VisualTreeHelper.GetClip () method would return the geometry of the contour.
But in my attempts, it always returns null.
Perhaps I am using it incorrectly, or it is not suitable for this purpose at all.
My attempt at getting the outlines:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace CopyGeometryElements
{
public partial class CopyGeometryElementsWindow : Window
{
public CopyGeometryElementsWindow()
{
InitializeComponent();
}
private void OnCopy(object sender, RoutedEventArgs e)
{
foreach (UIElement uie
in source.Children)
{
// Always returns null
var geometry = VisualTreeHelper.GetClip(uie);
var offset = uie.TranslatePoint(new Point(), source);
var copy = new Path()
{
Data=geometry,
Stroke = Brushes.Red,
StrokeThickness=2
};
Canvas.SetLeft(copy, offset.X);
Canvas.SetTop(copy, offset.Y);
target.Children.Add(copy);
}
}
}
}
<Window x:Class="CopyGeometryElements.CopyGeometryElementsWindow"
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:CopyGeometryElements"
mc:Ignorable="d"
Title="CopyGeometryElementsWindow" Height="450" Width="800">
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid x:Name="source"
Margin="10" Background="White">
<Border BorderBrush="Green" BorderThickness="2"
CornerRadius="15"
Width="40" Height="60" Margin="10"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Ellipse Stroke="Green" StrokeThickness="2"
Width="40" Height="60"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="200,100,0,0"/>
<Polygon Stroke="Green" StrokeThickness="2"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="100,150,0,0"
Points="0,0 100,100 0,200 50,100">
</Polygon>
</Grid>
<Button Grid.Column="1" Padding="15 5"
HorizontalAlignment="Center" VerticalAlignment="Center"
Click="OnCopy">
<TextBlock Text="Copy the geometry of all shapes from the left panel to the right"
Width="80"
TextWrapping="Wrap"
TextAlignment="Center"/>
</Button>
<Canvas x:Name="target" Grid.Column="2" Background="White"
Margin="10"/>
</Grid>
</Window>
As im sure this code just getting ur Clip.
For Example:
<Button Height="35" Width="75">
<Button.Clip>
<GeometryGroup>
<RectangleGeometry Rect="0, 0, 100, 100"/>
</GeometryGroup>
</Button.Clip>
</Button>
In this case you can get this Clip.
var figure = VisualTreeHelper.GetClip(uie);
But your Clip is Empty, consequently your result will be null

WPF Nested Popup does not close

I have a window with an Image that shows a Popup from its MouseEnter event handler:
private void theImage_MouseEnter(object sender, MouseEventArgs e)
{
contolPopup.IsOpen = true;
}
There is a button in this contolPopup that shows another Popup from its Click event handler:
private void setButton_Click(object sender, RoutedEventArgs e)
{
paramPopup.IsOpen = true;
}
This paramPopup behaves a bit strange just after it is shown: it does not close if I click somewhere outside it, but if I click on some control inside this paramPopup and then click somewhere outside, it closes fine.
What can cause this behavior?
I tried to focus paramPopup, but this does not help:
private void setButton_Click(object sender, RoutedEventArgs e)
{
paramPopup.IsOpen = setButton.IsChecked??false;
paramPopup.Focus();
}
Popup.StaysOpen is set to false for both Popups. I use .NET 4.0
Below I provided full source code:
fist popup is opened from a window:
<Window x:Class="GeoControls.WebcamWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GeoControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
SizeChanged="Window_SizeChanged" Loaded="Window_Loaded" SizeToContent="WidthAndHeight" ResizeMode="NoResize"
Title="{Binding Name}"
MouseEnter="theImage_MouseEnter" MouseLeave="theImage_MouseLeave" MouseLeftButtonUp="Window_MouseLeftButtonUp" Closed="Window_Closed">
<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.ColumnSpan="2" Name="theImage" Width="{Binding CurrentResolution.Width}" Height="{Binding CurrentResolution.Height}" />
<TextBlock Name="waitingTextBlock" Grid.Row="0" Text="Waiting the response from TV Server..." Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Popup Name="contolPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=theImage}" PopupAnimation="Scroll" Placement="Left" AllowsTransparency="True">
<Border CornerRadius="10" BorderThickness="1" BorderBrush="Black" Padding="10" Background="White">
<local:WebcamControl x:Name="webcamControl" Background="White">
</local:WebcamControl>
</Border>
</Popup>
</Grid>
</Window>
the second popup is opened by ParamButtons contained in WebcamControl contained in the first popup:
<UserControl x:Class="GeoControls.ParamButtons"
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"
xmlns:local="clr-namespace:GeoControls"
d:DesignHeight="30" d:DesignWidth="300">
<Grid>
............ omitted some code here................
<Popup Name="paramPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=setButton}" AllowsTransparency="True" Closed="paramPopup_Closed">
<Border Focusable="True" CornerRadius="10" BorderThickness="1" BorderBrush="Black" Padding="10" Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<local:ParamSlider Grid.Column="0" x:Name="paramSlider" />
<Button Content="OK" Click="okButton_Click" Grid.Column="1" Margin="5" />
<Button Content="Cancel" Click="cancelButton_Click" Grid.Column="2" Margin="5" />
</Grid>
</Border>
</Popup>
</Grid>
</UserControl>

Command binding is not working

I have a user control where one text box and button is there
<UserControl x:Class="Search.View.SearchView"
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:viewModel="clr-namespace:Search.ViewModel" x:Name="SearchUserControl"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="250" DataContext="{DynamicResource SearchViewModel}">
<UserControl.Resources>
<viewModel:SearchViewModel x:Key="SearchViewModel"></viewModel:SearchViewModel>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Right">
<TextBox x:Name="TxtSearch" Height="20" Width="100"></TextBox>
<Button x:Name="BtnSearchButton" Content="Search" Height="20" Command="{Binding SearchCommand}" Width="Auto" Margin="5,0,5,0" ></Button>
</StackPanel>
</Grid>
</UserControl>
in in my ViewModel file :
public RelayCommand SearchCommand { get; set; }
public SearchViewModel()
{
SearchCommand = new RelayCommand(TestSearch);
}
public void TestSearch(object p)
{
}
when i am using this user control on any view . Command binding is not working any help will be highly appreciated

Graphics from ScrollViewer overlays graphics outside ScrollViewer when scrolled down (WPF)

I have a XAML file. In this XAML file I have a ScrollViewer to which later I add WinFormsHost -> basically place where you can put standard WinForm. When I scroll the ScrollViewer down the graphics from inside of it goes outside. Please look at the first an the second screenshot. How to deal with that drawing problem? Can I set ZIndex for that ScrollViewer?
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Example"
Title="MainWindow" WindowState="Maximized" Loaded="Window_Loaded"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="3*" />
<RowDefinition Height="7*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" /> <!-- this column is for future purpouses -->
<ColumnDefinition Width="1366" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border BorderBrush="Red" BorderThickness="4" Grid.Column="0" Grid.Row="0">
<ListBox x:Name="ListBox"
</ListBox>
</Border>
<Button x:Name="AcceptButton" Grid.Row="1" ></Button>
</Grid>
<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2">
<ScrollViewer Grid.Column="1" Grid.Row="2" >
<WrapPanel x:Name="WinFormsPanel" Grid.Column="1" Grid.Row="2">
</WrapPanel>
</ScrollViewer>
</Border>
</Grid>
</Window>
AT THE BEGINNING
SCROLLED DOWN - WHEN SCROLLED UP EVERYTHING GOES BACK TO NORMAL
**EDIT ** Tried, but no difference:
<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2" ClipToBounds="True">
<ScrollViewer ClipToBounds="True" Grid.Column="1" Grid.Row="2" >
<WrapPanel ClipToBounds="True" x:Name="VideoPanel" Grid.Column="1" Grid.Row="2">
</WrapPanel>
</ScrollViewer>
</Border>
EDIT 2:
XAML
<Window x:Class="HomeSecurity.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HomeSecurity"
Title="MainWindow" WindowState="Maximized" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions>
<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2">
<ScrollViewer >
<WrapPanel x:Name="VideoPanel" >
</WrapPanel>
</ScrollViewer>
</Border>
<Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
MAINWINDOW.XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HomeSecurity {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
add(); add(); add();
}
private void add() {
WindowsFormsHost formsHost = new WindowsFormsHost();
VideoStream videoStream = new VideoStream();
formsHost.Width = 400;
formsHost.Height = 400;
formsHost.Child = videoStream;
Border lineBorder = new Border();
lineBorder.BorderBrush = Brushes.Green;
lineBorder.BorderThickness = new Thickness(2);
lineBorder.Child = formsHost;
VideoPanel.Children.Add(lineBorder);
}
}
}
There has to be added reference to System.WindowsFormsIntegration, System.Windows.Forms, System.Drawing.
You clearly have some problem there and you should thoroughly investigate what is going on with your code to try to rectify the situation optimally. However, the code that you have shown here is not to blame. I tried it in a new project and it works as expected. As there is nothing inside the WrapPanel, there is no content in the ScrollViewer to cause your problem. When adding code to demonstrate a problem, you should ensure that your code does demonstrate the problem.
Even so, without being able to recreate your problem, there might just be a quick fix that you can use in this case. You can just set the UIElement.ClipToBounds property on the Border to True and this will clip anything that extends beyond its bounds.
<Border ClipToBounds="True" BorderBrush="Green" BorderThickness="2" ...>
<ScrollViewer Grid.Column="1" Grid.Row="2" >
<WrapPanel x:Name="WinFormsPanel" Grid.Column="1" Grid.Row="2">
</WrapPanel>
</ScrollViewer>
</Border>

How to use content of derived controls in silverlight

In my project lots of views must have Ok,Cancel button to bottom of view. So i want create a base control. And add Ok, Cancel button to bottom of this control. Then i will inherite this control. In the inherited control i want to ad a textbox to near this buttons. How can i do this?
I believe there is a template for the base control.Is so in the inherited control's template edit the button panel and add textbox.
You can create Control which is inherited from ContentControl and define your buttons in ControlTemplate of it. Then you can just use this control as shown in sample.
ControlWithButtons.cs
[TemplatePart(Name="btnOk", Type= typeof(Button))]
public class ControlWithButtons : ContentControl
{
public ControlWithButtons()
{
this.DefaultStyleKey = typeof(ControlWithButtons);
}
Button _btnOk;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_btnOk = GetTemplateChild("btnOk") as Button;
if (_btnOk != null)
{
// do what you want with you button
_btnOk.Click += new RoutedEventHandler(_btnOk_Click);
}
}
void _btnOk_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Ok button clicked");
}
}
Generic.xaml (must be in (ProjectDir)/Themes/Generic.xaml)
(do not forget xmlns:local="clr-namespace:Test")
<Style TargetType="local:ControlWithButtons">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ControlWithButtons">
<Border Background="Yellow" CornerRadius="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Border Margin="10" Background="LightGray">
<ContentPresenter/>
</Border>
<Button Grid.Row="1" x:Name="btnOk" Content="OK" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Using your control:
<Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<local:ControlWithButtons Width="300" Height="250" HorizontalAlignment="Center" VerticalAlignment="Center">
<!-- TextBox is put into control -->
<TextBox Width="200" Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" />
<!-- You can also specify ContentTemplate for ControlWithButtons -->
<!-- (see in MSDN "http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate(v=vs.95).aspx") -->
</local:ControlWithButtons>
</Grid>
I found the solution. [ContentProperty("")] attribute solved my problem.
Like this:
Base Xaml
<Grid Name="layoutRoot" VerticalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="289*" />
<RowDefinition Height="51" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Orientation="Horizontal">
<Button Name="btnOk" Content="Ok" Height="23" Width="75" HorizontalAlignment="Left" Margin="10,10,10,10" Command="{Binding Path=OnApply, Mode=OneWay}"/>
<Button Name="btnCancel" Content="Cancel" Height="23" Width="75" HorizontalAlignment="Left" Margin="10,10,10,10"/>
</StackPanel>
<Grid Name="rootContent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" />
</Grid>
</sdk:ChildWindow>
Base CodeBehind
[ContentProperty("RootContentControl")]//This attribute solved my problem.
public partial class BaseView : ChildWindow
{
public BaseView()
{
InitializeComponent();
}
public UIElementCollection RootContentControl
{
get { return rootContent.Children; }
}
}
Inherited Xaml
<Views:BaseView x:Class="MvvmLight1.Views.InheritedView"
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:Views="clr-namespace:MvvmLight1.Views"
xmlns:viewModels="clr-namespace:MvvmLight1.ViewModel"
mc:Ignorable="d" d:DesignHeight="244" d:DesignWidth="392"
DataContext="{Binding Source=viewModels:InheritedViewModel}">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
</Grid>
</Views:BaseView>
Inherited CodeBehind
public partial class InheritedView : BaseView
{
public InheritedView()
{
InitializeComponent();
}
}

Resources