I have got an userControl which contains a textblock and a textbox, but I am stuck trying to pass arguments to it via DependencyProperties.
here is some stuff:
the userControl XAML:
<UserControl x:Class="WpfApp1.Controls.InfoField"
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:WpfApp1.Controls"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<DockPanel>
<TextBlock Text="{Binding TextInBlock}" HorizontalAlignment="Right" Margin="2" Width="160"></TextBlock>
<TextBox Text="{Binding BindingInBox}" Grid.Column="1" Width="200" Margin="2"></TextBox>
</DockPanel>
</Grid>
</UserControl>
the associated code behind:
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1.Controls
{
/// <summary>
/// Logique d'interaction pour InfoField.xaml
/// </summary>
public partial class InfoField : UserControl
{
public static readonly DependencyProperty TextInBlockProperty =
DependencyProperty.Register("TextInBlock", typeof(string), typeof(TextBlock));
public static readonly DependencyProperty BindingInBoxProperty =
DependencyProperty.Register("BindingInBox", typeof(string), typeof(TextBox));
public string TextInBlock
{
get => (string) GetValue(TextInBlockProperty);
set => SetValue(TextInBlockProperty, value);
}
public string BindingInBox
{
get => (string)GetValue(BindingInBoxProperty);
set => SetValue(BindingInBoxProperty, value);
}
public InfoField()
{
InitializeComponent();
this.DataContext = this;
}
}
}
and the MainWindow XAML:
<Grid ColumnSpan="4" Row="1" Name="detailsGrid"
DataContext="{Binding ElementName=listBoxBooks,Path=SelectedItem}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
------> <controls:InfoField Grid.ColumnSpan="2" BindingInBox="{Binding Author,ElementName=mainWindow}" TextInBlock="Auteur :"></controls:InfoField>
<Button Grid.Row="0" Grid.Column="2" Click="ButtonBase_OnClick">Click me!</Button>
<TextBlock Text="Titre :" HorizontalAlignment="Right" Grid.Row="1"></TextBlock>
<TextBox Text="{Binding Title}" Grid.Column="1" Grid.Row="1" Width="200"></TextBox>
</Grid>
at the time being, visual studio gives me an error, at the line at which I put a text arrow; the error says : "the 'TextInBlock' property is already registered by 'TextBlock'". I don't understand this error message. Please note that there is a difference between the 2 bindings, one references an other field (also via binding, the field being 'Author') whereas the other is just a simple string.
thank you.