Binding sur textbox dans usercontrol
Bonjour à tous,
Je ne poste pas souvent, mais la je suis sec.
Tout d'abord, merci à ceux qui lieront mon message et passeront du temps à tenter de m'aider!
Je vais donc tenter de vous expliquer mon problème.
J'ai une grid, dans laquelle j'ai une ListBox qui utilise un datatemplate dans lequel un usercontrol perso.
Mon usercontrol est bindé sur le datacontext de la grid.
Mon datacontext contient une observablecollection dans laquel j'ai des objets Piece
Ma class Piece est interfacé avec INotifyPropertyChanged
Classe pièce :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Piece : INotifyPropertyChanged
{
private string m_sNom;
public string Nom
{
get
{
// Return
return m_sNom;
}
set
{
// Set
m_sNom = value;
FirePropertiyChanged("Nom");
}
}
public Piece(string p_sNom)
{
m_sNom = p_sNom;
}
public event PropertyChangedEventHandler PropertyChanged;
public void FirePropertiyChanged(string propertyName)
{
// Si la propriété à changé
if (PropertyChanged != null)
// Mettre à jour la propriété
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
} |
Ma page xaml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <UserControl x:Class="Bolt.UsrControl.ucAssembly"
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:ESLWPF="clr-namespace:ESL;assembly=ESL"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="PieceTemplate">
<Border HorizontalAlignment="Stretch" BorderBrush="Blue" BorderThickness="1" CornerRadius="2" Width="auto">
<Grid HorizontalAlignment="Stretch" DataContext="{Binding}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ESLWPF:ESLWPFTextBox Grid.Column="0" Text="{Binding Nom}" Margin="3" NulAutorise="False" />
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox Name="m_lbxPiece" Grid.Row="0" ItemTemplate="{DynamicResource PieceTemplate}" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" Grid.ColumnSpan="2">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</UserControl> |
Le xaml de mon usercontrol
Code:
1 2 3 4 5 6 7 8 9 10 11
| <UserControl x:Class="ESL.ESLWPFTextBox"
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 x:Name="LayoutRoot">
<TextBox Name="m_txt" Text="{Binding Text, Mode=TwoWay}" />
</Grid>
</UserControl> |
Le code de mon usercontrol
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public partial class ESLWPFTextBox : UserControl
{
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ESLWPFTextBox));
public string Text
{
get
{
// Return
return (string)GetValue(TextProperty);
}
set
{
// Set
SetValue(TextProperty, value);
}
}
public ESLWPFTextBox()
{
// Initialiser le composant
InitializeComponent();
// Mettre à jour le DataContext
LayoutRoot.DataContext = this;
}
} |
Mon problème : J'arrive bien à afficher les données dans mon controls dans ma listebox, mais lorsque je tente de modifier ces données, elles ne sont pas mise à jour dans mon observablecollection.
J'espère avoir était assez clair !
Merci pour votre aide