Default value for property of UserControl - c#

UserControl contains BorderBrush property derived from Control. How can I setup it's default value, for example, to Brushes.Black and make it available to be set by developer who will use my control?
I tried to assign initial value in <UserControl> tag in control's xaml file and in constructor of it, but when I do any of this, value assigned for the control
externally is ignored.

You would usually do this by overriding metadata for the BorderBrush property in your UserControl derived class:
public partial class MyUserControl : UserControl
{
static MyUserControl()
{
BorderBrushProperty.OverrideMetadata(
typeof(MyUserControl),
new FrameworkPropertyMetadata(Brushes.Black));
}
public MyUserControl()
{
InitializeComponent();
}
}

Perhaps styles would be best. You could create a new UserControl, let's call it BorderedControl. I've created a new folder called Controls to hold it.
<UserControl x:Class="BorderTest.Controls.BorderedControl"
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>
</Grid>
</UserControl>
Next, create a Resource Dictionary, UserControlResources. Be sure to include the namespace of the control:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrls="clr-namespace:BorderTest.Controls">
<Style TargetType="{x:Type ctrls:BorderedControl}">
<Setter Property="BorderBrush" Value="Lime"/>
<Setter Property="BorderThickness" Value="3"/>
</Style>
</ResourceDictionary>
Here you can set what ever properties you'd like to have default.
Then, include the resource dictionary in the user control resources:
<UserControl x:Class="BorderTest.Controls.BorderedControl"
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">
<UserControl.Resources>
<ResourceDictionary Source="/BorderTest;component/Resources/UserControlResources.xaml"/>
</UserControl.Resources>
<Grid>
</Grid>
</UserControl>
Finally, add the control to your main window:
<Window x:Class="BorderTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrls="clr-namespace:BorderTest.Controls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ctrls:BorderedControl Width="100"
Height="100"/>
</Grid>
</Window>
Here is my solution:
Here is the application when you run it:
You can simply change the border of your user control with this:
<ctrls:BorderedControl Width="100"
Height="100"
BorderBrush="Orange"/>

Related

WPF: Can't change property of custom control inside other control template (+Pictures)

Let's create an empty WPF project.
Add a very simple UserControl (i named it MyUserControl):
<UserControl x:Class="Delete_This_Test.MyUserControl"
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"
Background="Red"
Height="100">
<Grid>
</Grid>
</UserControl>
As you can see, i have changed only 2 properties: Background and Height to "Red" and "100".
Put our created control in MainWindow:
<Window x:Class="Delete_This_Test.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:Delete_This_Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:MyUserControl Width="100"
Height="200"
Background="Blue">
</local:MyUserControl>
</Grid>
</Window>
Here, i have changed Width, Height and Background to "100", "200" and "Blue".
And it works: Without ControlTemplate Picture
But if we put MyUserControl in some ControlTemplate, for example of Button:
<Window x:Class="Delete_This_Test.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:Delete_This_Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<local:MyUserControl Width="100"
Height="200"
Background="Blue">
</local:MyUserControl>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Window>
This will not work. Only Width property will changed, because we didn't set it in MyUserControl xaml.
Height and Background will be the same as "100" and "Red":
With ControlTemplate Picture
So my question is: Is it bug of WPF, or i'm missing something obvious?
*
Because i need to use one custom control in different templates, and change some properties, e.g. Background of control

Where can I put the EventSetter so that its handler is searched in the Control's code-behind, not in the resource dictionary's?

I have a template with a Style that uses an EventSetter to set a common event handler to some Hyperlinks. But I want to handle this event not in the resource dictionary's .cs file but in the custom control's .cs file. How can I do this? I am in the process of moving resources into theme .xaml files. I thought about separating the functionality part of the Hyperlink style, but where should I put the event setter? I thought I can use commands, but is there a more concise tehnique that does not require changing each Hyperlink element and works for elements that do not support commands?
I use .NET Framework 4.7.2. I have made a few searches over the web and a simple test example:
App.xaml
<Application x:Class="wpf_test_2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf_test_2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Dictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf_test_2">
<ControlTemplate x:Key="MyControlTemplate">
<ControlTemplate.Resources>
<Style TargetType="Button">
<!-- obviously, this does not compile: -->
<EventSetter Event="Click" Handler="MyHandler"/>
</Style>
</ControlTemplate.Resources>
<UniformGrid Rows="5" Columns="5">
<Button>A</Button>
<Button>B</Button>
<Button>C</Button>
<Button>D</Button>
<Button>E</Button>
<Button>F</Button>
<Button>G</Button>
<Button>H</Button>
<Button>I</Button>
</UniformGrid>
</ControlTemplate>
</ResourceDictionary>
MainWindow.xaml
<Window x:Class="wpf_test_2.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:wpf_test_2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:MyControl/>
</Grid>
</Window>
MyControl.xaml
<Control x:Class="wpf_test_2.MyControl"
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:wpf_test_2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Template="{DynamicResource MyControlTemplate}">
</Control>
MyControl.xaml.cs
/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl : Control
{
public MyControl()
{
InitializeComponent();
}
private void MyHandler(object sender, RoutedEventArgs e)
{
var b = sender as Button;
MessageBox.Show($"Button {b.Content.ToString()} was clicked!");
}
}
Screenshot
Expected: the project compiles, and when I click one of the buttons, a MessageBox appears with the content of the button as string.
Actual: the project does not compile.
Thank you.
Inspired by Clemens' comment, I created a RoutedUICommand, a corresponding command binding, and setters which set Command and CommandParameter:
AboutWindow.xaml
<Window
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:cs_timed_silver"
x:Class="cs_timed_silver.AboutWindow"
mc:Ignorable="d"
Width="448" Height="200" ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
Loaded="Window_Loaded"
Closed="Window_Closed"
Style="{DynamicResource AboutWindowStyle}">
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:AboutWindow.NavigateCommand}"
CanExecute="CommandBinding_CanExecute"
Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
</Window>
AboutWindow.xaml.cs
public partial class AboutWindow : Window
{
public static RoutedUICommand NavigateCommand { get; set; } =
new RoutedUICommand("", "NavigateCommand", typeof(AboutWindow));
[...]
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo((e.Parameter as Uri).AbsoluteUri));
}
Light.xaml
<Style x:Key="AboutWindowStyle" TargetType="local:AboutWindow">
[...]
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<ControlTemplate.Resources>
[...]
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="Command" Value="{Binding NavigateCommand}"/>
<Setter Property="CommandParameter"
Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=NavigateUri}"/>
</Style>
</ControlTemplate.Resources>
[...]

Window in Window Dynamic Content

I am a little new to WPF.
I want to use one window for all my content, the content will vary dramatically and the Window.Resources content will need to vary also.
<Window x:Class="NS01.WPF01"
xmlns:b="clr-namespace:NS01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:FluidKit.Showcase.ElementFlow"
xmlns:Controls="clr-namespace:FluidKit.Controls;assembly=FluidKit"
Loaded="Window_Loaded"
Title="NS01"
WindowStartupLocation="CenterScreen"
Width="1280"
Height="720"
WindowStyle="ThreeDBorderWindow">
<Window.Resources>
...
</Window.Resources>
<Grid>
...
</Grid>
</Window>
As an example, I would like to use some third party content like FluidKit's ElementFlow.
There are many good examples of how to dynamically Load UserControls, but nothing on Window in Window. The best I have found is: Changing content dynamically in wpf window
I would like to have multiple *.xaml Files in my project and load content dynamically into the Current Window.
EDIT:
I like WPF Frame, but I can not load and unload Window.Resources:
<Window x:Class="NS01.WPF01"
xmlns:b="clr-namespace:NS01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:FluidKit.Showcase.ElementFlow"
xmlns:Controls="clr-namespace:FluidKit.Controls;assembly=FluidKit"
Loaded="Window_Loaded"
Title="NS01"
WindowStartupLocation="CenterScreen"
Width="1280"
Height="720"
WindowStyle="ThreeDBorderWindow">
<Window.Resources>
...
</Window.Resources>
<Frame Name="ContentHolder" />
</Window>
ContentHolder.Source = new Uri("/XAML/Repository/Page1.xaml", UriKind.Relative);
Just so you know, I have explored:
SolidColorBrush SolidColorBrushRed = new SolidColorBrush(Colors.Red);
this.Resources.Add("RedBrushResource", SolidColorBrushRed);
Is what I am wanting to do possible? Can I load all the required content from a single xaml File?
Thanks.
EDIT:
This is one method, but not really what I was wanting:
<Page x:Class="NS01.XAML.Repository.Page1"
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:NS01.XAML.Repository"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<TextBlock Text="Welcome" FontSize="18" Foreground="{DynamicResource ResourceKey=textForeColorResource}" Margin="80,32,66,219"/>
</Grid>
</Page>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NS01.XAML.Resources">
<SolidColorBrush x:Key="textForeColorResource" Color="Blue"/>
</ResourceDictionary>
ContentHolder.Source = new Uri("/XAML/Repository/Page1.xaml", UriKind.Relative);
ResourceDictionary RD = new ResourceDictionary()
{
Source = new Uri("/NS01;component/XAML/Resources/Page1.xaml", UriKind.RelativeOrAbsolute)
};
Application.Current.Resources.MergedDictionaries.Add(RD);
Application.Current.Resources.MergedDictionaries.Remove(RD);
foreach (var R in MWindow.Resources)
{
MessageBox.Show(R.ToString());
}

Showing a control based on type of databound viewmodel?

I'm learning WPF at the moment. I'm finding xaml quite tough to use. I have MainWindow.xaml defined like this:
<Window x:Class="Compliance.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="MainWindow.Resources.xaml"></ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
And MainWindow.Resources.xaml like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Compliance.ViewModel"
xmlns:vw="clr-namespace:Compliance.View">
<DataTemplate DataType="{x:Type vm:Entities.AbstractEntityViewModel}">
<vw:AbstractEntityView></vw:AbstractEntityView>
</DataTemplate>
</ResourceDictionary>
AbstractEntityView is like this:
<UserControl x:Class="Compliance.View.AbstractEntityView"
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">
<StackPanel>
<Label Content="ID:"></Label>
<TextBlock Text="{Binding Path=EntityId}"></TextBlock>
</StackPanel>
</UserControl>
Then in App.xaml.cs I am overriding OnStartup like this:
MainWindow window = new MainWindow();
//Model class
Individual ind = new Individual(1,"Name");
//subclass of AbstractEntityViewModel
var vm = new Entities.IndividualEntityViewModel(ind);
window.DataContext = vm;
window.Show();
However, nothing appears in the window.
I used the answer from this question to get my control to render. However, this requires you to refer to elements in the view from the code, which I don't want to do.
Is it possible to get a window to pick a View to render based on the ViewModel set as its datacontext? Or do I have the wrong idea about how MVVM is supposed to work?
You have the right idea, but you're not actually telling WPF to display your ViewModel anywhere
I usually host a ViewModel in a ContentControl object if I am binding to a single ViewModel
<Window x:Class="Compliance.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="MainWindow.Resources.xaml"></ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding }" />
</Grid>
</Window>
The ContentControl is usually not needed for lists of Models or ViewModels, since the object is automatically inserted as the Content property of the ContentPresenter of each item. For example, no ContentControl is needed when binding a ListBox to a collection of ViewModels
<ListBox ItemsSource="{Binding MyCollectionOfViewModel}" />

How to instantiate DataContext object in XAML

I want to be able to create an instance of the DataContext object for my WPF StartupUri window in XAML, as opposed to creating it code and then setting the DataContext property programmaticly.
The main reason is I don't need to access the object created externally and I don't want to have to write code behind just for setting the DataContext.
I'm sure I've read somewhere how to instantiate the DataContext object in XAML but I can't find it in any of the usual places...
You add an XML namespace for whatever namespace your DataContext lives in, create an instance of it in the Window Resources and set the DataContext to that resource:
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:MyViewModel x:Key="MyViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MyViewModel}">
</Grid>
</Window>
You can just specify this directly in XAML for the entire Window:
<Window
... xmlns definitions ...
>
<Window.DataContext>
<local:CustomViewModel />
</Window.DataContext>
</Window>
This creates a view model named "CustomViewModel" in the namespace aliased to local, directly as the DataContext for the Window.
Assuming this code:
public abstract class BaseView { }
public class RuntimeView : BaseView { }
public class DesigntimeView : BaseView { }
Try this:
<Page.DataContext>
<local:RuntimeView />
</Page.DataContext>
<d:Page.DataContext>
<local:DesigntimeView />
</d:Page.DataContext>
<ListBox ItemsSource="{Binding}" />
Best of luck!
If you need to set the DataContext as same control class:
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
</Window>
use RelativeSource binding.
or just
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="350" Width="525"
>
<Window.DataContext>
< new instance of any viewModel here....>
</Window.DataContext>
</Window>
If want to assign an instance of different class than itself.

Categories

Resources