I have an application in c# WPF MVC. My objective is to create a title bar and call it for all my windows.
I created this :
XAML:
<Grid x:Class="Views.TitleBarView"
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:Views"
mc:Ignorable="d"
Style="{DynamicResource TitleStyleGrid}"
x:Name="barView">
<Label x:Name="labelAppName" Style="{DynamicResource TitleStyleLabel}" Content="{Binding Content, ElementName=barView}"/>
<Button x:Name="bttnClose" Style="{DynamicResource ButtonStyleCloseWindow}" Command="{Binding CloseCommand}"/>
</Grid>
c#:
public partial class TitleBarView : Grid
{
static TitleBarView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleBarView), new FrameworkPropertyMetadata(typeof(TitleBarView)));
}
public readonly static DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(string), typeof(TitleBarView), new PropertyMetadata(""));
public string Content
{
get { return (string)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public TitleBarView()
{
InitializeComponent();
TitleBarViewModel tvm = new TitleBarViewModel();
tvm.RequestClose += (s, e) => this.Close();
DataContext = tvm;
}
private void Close()
{
Window.GetWindow(this).Close();
}
}
I have created the property Content
for my Grid
and the label
inside bind this property. So when I call my class TitleBarView
I just have to set property `Content``and the label automatically update.
It works good when I directly set content with String :
<Window [...]
xmlns:local="clr-namespace:VectorReaderV3.Views"
[...]>
<local:TitleBarView x:Name="titleBar" Content="My Title"/>
<Window/>
But with binding, I have an empty title :
<Window [...]
xmlns:local="clr-namespace:VectorReaderV3.Views"
[...]>
<local:TitleBarView x:Name="titleBar" Content="{Binding WindowTitle}">
<Window/>
What did I make wrong ?