I've a windows that should be the same between 2 applications, except some points.
I wanted to inherit from it to create a subclass(which has no XAML), that only does some customization(like window's title) in the constructor.
Is this possible?
Is this possible?
Yes.
Create a class that inherits from System.Windows.Window:
public class YourBaseClass : Window
{
public YourBaseClass() : base()
{
Title = "Common Title";
}
}
...and change the base class of your windows to use this one, both in the code-behind:
public partial class MainWindow : YourBaseClass
{
public MainWindow()
{
InitializeComponent();
}
}
...and in XAML:
<local:YourBaseClass x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Height="300" Width="300">
</local:YourBaseClass>
Unlike window forms, WPF have no visual inheritance. You may refer to this post for why.
Therefore, we cannot use inheritance here. However, you can add custom property to your windows class and made the customization based on the property.
Let say I want to create two similar windows with different "Title" ( this is just an example, i do know window has a Title property):
in xaml.cs:
public WindowTest //constructor
{
InitializeComponent();
this.Loaded+=WindowTest_Loaded;
}
private void WindowTest_Loaded(object sender, RoutedEventArgs e)
{
//Please note that custom property wont be set until Windows is loaded.
//Your customization here
this.Title = TitleText;
}
public string TitleText
{
get { return GetValue( Property1Property ).ToString(); }
set { SetValue( TitleTextProperty, value ); }
}
// Using a DependencyProperty as the backing store for Property1.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleTextProperty
= DependencyProperty.Register(
"TitleText",
typeof( string ),
typeof( WindowTest ),
new PropertyMetadata( String.Empty )
);
in xaml
<Grid>
<test:WindowText TitleText = "Hello World"/> //first window
<test:WindowText TitleText = "I Hate You"/> //second window
</Grid>
Related
WPF Data binding doesnt work for custom controls that are defined inside a xaml collection tag. I just want to define a collection of custom widgets inside a custom control and bind some widgets properties against viewmodel properties. Like so.
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<local:MyCustomControl>
<local:MyCustomControl.Widgets>
<local:MyCustomWidget ImportantToggle="{Binding SomeToggle}"/>
</local:MyCustomControl.Widgets>
</local:MyCustomControl>
</Grid>
</Window>
That is my custom control. I use an obseravblecollection for the widgets and call SetValue in the constructor to get propertychanged callback later (right now not used in example)
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
namespace WpfApp1
{
public class MyCustomControl : FrameworkElement
{
public ObservableCollection<MyCustomWidget> Widgets
{
get { return (ObservableCollection<MyCustomWidget>)this.GetValue(WidgetsProperty); }
set { this.SetValue(WidgetsProperty, value); }
}
public static DependencyProperty WidgetsProperty = DependencyProperty.Register("Widgets", typeof(ObservableCollection<MyCustomWidget>), typeof(MyCustomControl), new PropertyMetadata(null, (e, args) => ((MyCustomControl)e).WidgetsChanged(args)));
public void WidgetsChanged(DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("widgets collection object changed inside my custom control!");
}
public MyCustomControl()
{
this.SetValue(WidgetsProperty, new ObservableCollection<MyCustomWidget>());
}
}
}
and that is my custom widget:
namespace WpfApp1
{
public class MyCustomWidget : FrameworkContentElement
{
public bool ImportantToggle
{
get { return (bool)this.GetValue(ImportantToggleProperty); }
set { this.SetValue(ImportantToggleProperty, value); }
}
public static DependencyProperty ImportantToggleProperty = DependencyProperty.Register("ImportantToggle", typeof(bool), typeof(MyCustomWidget), new PropertyMetadata(false, (e, args) => ((MyCustomWidget)e).ImportantToggleChanged(args)));
public void ImportantToggleChanged(DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("my toggle changed inside my custom widget!");
}
}
}
And finally my simplistic ViewModel:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApp1
{
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool _someToggle;
public bool SomeToggle
{
get { return this._someToggle; }
set
{
this._someToggle = value;
this.NotifyPropertyChanged();
}
}
public MainViewModel()
{
this.SomeToggle = !this.SomeToggle;
}
}
}
Thats the output I get from Debug.Writeline: widgets collection object changed inside my custom control!
Observation: I cant bind against properties of MyCustomWidget. I understand that the binding might fail in this scenario because the observablecollection is created inside of the constructor of mycustomcontrol, but I dont know how to fix it to get the binding working inside mycustomwidget.
For that binding to work, your local:MyCustomWidget needs to have the same DataContext as the main window. WPF elements inherit their logical parent's DataContext. MyCustomWidget doesn't, because it's not in the logical tree. It's just sitting there. You're not adding it to any kind of normal child collection of its parent, just to a random ObservableCollection that the framework doesn't know about.
The code below is probably a crude hack. I haven't investigated this corner of WPF. I urge you with the utmost sincerity to find out the right way of doing this. But with this addition to your code, I hit the propertychanged event in MyCustomWidget when the binding is initialized.
public MyCustomControl()
{
this.SetValue(WidgetsProperty, new ObservableCollection<MyCustomWidget>());
Widgets.CollectionChanged += Widgets_CollectionChanged;
}
private void Widgets_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems is System.Collections.IEnumerable)
{
foreach (MyCustomWidget widget in e.NewItems)
{
AddLogicalChild(widget);
}
}
}
By the way, you can save the trouble of toggling the toggle in the MainViewModel constructor. That happens long before the binding exists. I added a checkbox instead:
<StackPanel>
<CheckBox IsChecked="{Binding SomeToggle}">Test Toggle</CheckBox>
<local:MyCustomControl>
<local:MyCustomControl.Widgets>
<local:MyCustomWidget
ImportantToggle="{Binding SomeToggle}"
/>
</local:MyCustomControl.Widgets>
</local:MyCustomControl>
</StackPanel>
Update:
This omits your Widgets collection entirely, and the binding works without any effort on our part. The child widgets will be in MyCustomControl.Children. Importantly that we aren't limiting the child type to MyCustomWidget any more. That's a significant design change, and may not fit your requirements. You could examine the Panel class closely, and write a class that works the same way, but accepts only one type of child (that would mean writing an analog of UIElementCollection, which will be mostly a big pile of tedious boilerplate).
MyCustomControl.cs
[ContentProperty("Children")]
public class MyCustomControl : Panel
{
}
MyCustomWidget.cs
public class MyCustomWidget : Control
{
public bool ImportantToggle
{
get { return (bool)this.GetValue(ImportantToggleProperty); }
set { this.SetValue(ImportantToggleProperty, value); }
}
public static DependencyProperty ImportantToggleProperty =
DependencyProperty.Register("ImportantToggle", typeof(bool), typeof(MyCustomWidget),
new PropertyMetadata(false, (e, args) => ((MyCustomWidget)e).ImportantToggleChanged(args)));
public void ImportantToggleChanged(DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("my toggle changed inside my custom widget!");
}
}
MainWindow.xaml
<local:MyCustomControl>
<local:MyCustomWidget
ImportantToggle="{Binding SomeToggle}"
/>
</local:MyCustomControl>
I have a dependency property in a class which inherits from Canvas, like so:
public partial class HueVisualizer : Canvas
{
public HueVisualizer()
{
InitializeComponent();
}
public decimal InnerHue
{
get { return (decimal)GetValue(HueProperty); }
set { SetValue(HueProperty, value); }
}
// Using a DependencyProperty as the backing store for InnerHue,Saturation and Luminance. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HueProperty =
DependencyProperty.Register("InnerHue", typeof(decimal), typeof(LuminanceVisualizer), new FrameworkPropertyMetadata((decimal)0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}
I'm trying to bind to it in Xaml like so:
<UserControl x:Class="Project1.UserControl1"
x:Name="TheControl"
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:project1="clr-namespace:Project1"
mc:Ignorable="d"
d:DesignHeight="120" d:DesignWidth="300">
...
<Grid Grid.Row="0" x:Name="HueGrid">
<project1:HueVisualizer x:Name="HueVisual"
InnerHue ="{Binding ElementName=TheControl, Path=Hue, Mode=TwoWay}"
Height="20"
Width="{Binding ElementName=TheControl, Path=Width}"/>
</Grid>
<UserControl />
For completeness, the properties I'm trying to bind from:
public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
public decimal Hue
{
get { return (decimal)GetValue(HueProperty); }
set { SetValue(HueProperty, value); }
}
...
// Using a DependencyProperty as the backing store for Hue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HueProperty =
DependencyProperty.Register("Hue", typeof(decimal), typeof(UserControl1), new
FrameworkPropertyMetadata((decimal)0));
...
}
However, when I try to run/debug the project, I get an exception on InitializeComponent() of UserControl1:
A 'Binding' cannot be set on the 'InnerHue' property of type 'HueVisualizer'. A
'Binding' can only be set on a DependencyProperty of a DependencyObject.
No matter how many times I look at examples, it seems to me that InnerHue should be a valid dependency property. I also double checked to make sure that Canvas is a DependencyObject (if it weren't, GetValue and SetValue should throw a compiler error). What in the world am I doing incorrectly? As I am relatively new to WPF, I can't help but feel that I'm missing something obvious.
You gave the wrong owner type for your DependencyProperty
you wrote LuminanceVisualizer and it should be HueVisualizer .
public static readonly DependencyProperty HueProperty =
DependencyProperty.Register("InnerHue", typeof(decimal),
typeof(LuminanceVisualizer), // Replace with HueVisualizer
new FrameworkPropertyMetadata((decimal)0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))
I have a User Control Library that I am loading dynamically. From that lib I am inserting a Tabitem into a TabControl. I can load the tab and show it without error. However, I can't seem to get the binding on the control working.
This is the code I use to load it and add it to the TabControl:
Assembly moduleAssembly = Assembly.Load("ControlLib");
UserControl uc = (UserControl)Application.LoadComponent(new System.Uri("/ControlLib;component/UserControl1.xaml", UriKind.RelativeOrAbsolute));
TabControl itemsTab = (TabControl)this.FindName("mainTabControl");
TabItem newTab = new TabItem();
newTab.Content = uc;
newTab.Header = "Test";
itemsTab.Items.Add(newTab);
itemsTab.SelectedItem = newTab;
This is the C# code for the control:
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty TestStringProperty =
DependencyProperty.Register("TestString", typeof(string), typeof(UserControl1));
public string TestString { get; set; }
public UserControl1()
{
InitializeComponent();
TestString = "Hello World";
}
}
This is the XAML code for the control:
<UserControl x:Class="ControlLib.UserControl1"
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>
<TextBox Height="30" Width="100" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=TestString, Mode=TwoWay}" />
</Grid>
</UserControl>
When the tab displays all I see if a blank in the TextBox rather than "Hello World"
What am I missing?
You would still be setting the DataContext of your user control to instance of the class. Just how you go about creating that instance differs as you would be loading that dll a runtime. But fundamentally the binding setup remains the same.
var assembly = Assembly.LoadFrom(#"yourdllname.dll");
Type type = assembly.GetType("ClassLibrary1.SampleViewModel");
object instanceOfMyType = Activator.CreateInstance(type);
DataContext = instanceOfMyType;
For how basic databinding works read MSDN documentation.
Make sure you select the correct framework on the top of the screen.
EDIT
Usually this is created as a separate class (ViewModel in MVVM pattern).
public partial class Window3 : Window, INotifyPropertyChanged
{
public Window3()
{
InitializeComponent();
DataContext = this;
TestString = "Hello World.";
}
string _testString;
///<summary>Gets or sets TestString.</summary>
public string TestString
{
get { return _testString; }
set { _testString = value; OnPropertyChanged("TestString"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
var e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}
}
I have two UserControls (uc1 and uc2) loading into a third UserControl (shell). Shell has two properties, uc1 and uc2, of type UserControl1 and UserControl2, and each have a DependencyProperty registered to their own classes called IsDirty:
public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register("IsDirty", typeof (bool), typeof (UserControl1));
public bool IsDirty
{
get { return (bool) GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
(same code for UserControl2)
Shell has TextBlocks bound to the IsDirty properties:
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
<TextBlock Text="{Binding ElementName=shell, Path=Uc2.IsDirty}"/>
When I change the values of IsDirty in uc1 and uc2, Shell never gets notified. What am I missing? UserControl is descendant of DependencyObject...
The same behavior occurs if I have regular properties notifying changes via INotifyPropertyChanged.
If I raise a routed event from uc1 and uc2, bubbling up to Shell, then I can catch the Dirty value and everything works, but I shouldn't have to do that, should I?
Thanks
Edit: The answer is to raise property changed event on the Uc1 and Uc2 properties or make them DPs.
I tried reproducing your problem using a simple setup, and it works fine for me. I'm not sure though if this setup is correct enough to replicate your situation. Anyway, I'm posting it just in case. It might be helpful:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
x:Name="shell"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Click="Button_Click">Click</Button>
<TextBlock Text="{Binding ElementName=shell, Path=Uc1.IsDirty}"/>
</StackPanel>
</Window>
Code-Behind:
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MyUserControl uc1 = new MyUserControl();
public MyUserControl Uc1
{
get { return this.uc1; }
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.uc1.IsDirty = !this.uc1.IsDirty;
}
}
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
}
public bool IsDirty
{
get { return (bool)GetValue(IsDirtyProperty); }
set { SetValue(IsDirtyProperty, value); }
}
// Using a DependencyProperty as the backing store for IsDirty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDirtyProperty =
DependencyProperty.Register("IsDirty", typeof(bool), typeof(UserControl), new UIPropertyMetadata(false));
}
}
Karmicpuppet's answer works well. However it didn't solve my problem because Shell is also of type UserControl. For it to work I needed to raise the property changed on Uc1 and Uc2. When I declared them as DependencyProperties all worked as expected. Duh!
I have a user control which I would like to add a dependency property of type Func so I can assign it a method handler in XAML. However, this will cause an XAMLParseException: 'Func`2' type does not have a public TypeConverter class. What am I doing wrong? Do I need to implement a TypeConverter for Func or is there a better way?
The Func Dependency Property in the user control (MyUserControl):
public Func<int, int> MyFunc
{
get { return (Func<int, int>)GetValue(MyFuncProperty); }
set { SetValue(MyFuncProperty, value); }
}
public static readonly DependencyProperty MyFuncProperty =
DependencyProperty.Register("MyFunc",
typeof(Func<int, int>),
typeof(SillyCtrl),
new UIPropertyMetadata(null));
Example of using the DP, XAML:
<Window x:Class="FuncTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:FuncTest="clr-namespace:FuncTest"
Title="Window1" Height="300" Width="300">
<Grid>
<FuncTest:MyUserControl MyFunc="SquareHandler" />
</Grid>
</Window>
Code behind:
namespace FuncTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
SquareHandler = (arg => arg * arg);
DataContext = this;
}
public Func<int, int> SquareHandler { get; set; }
}
}
MyFunc="SquareHandler"
Means set "MyFunc" property to a "SquareHandler" string and that's why it asks you for a TypeConverter able to converting strings into Funcs, change it to
<FuncTest:MyUserControl MyFunc="{Binding SquareHandler}" />
to use SquareHandler property of the current DataContext.