code XAML équivalent au code Behind d'un binding
Bonjour,
Pourriez-vous me donner l'équivalent de ce code Behind d'un binding en XAML?
voici le code de mon UserControl. Je lie le texte de ma textbox à ma Dependency Property Valeur.
Code Behind
--------------
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Verification_Lotto
{
/// <summary>
/// Interaction logic for NumericUpDown.xaml
/// </summary>
public partial class NumericUpDown : UserControl
{
Binding bind;
public int Valeur
{
get { return (int)GetValue(ValeurProperty); }
set { SetValue(ValeurProperty, value); }
}
public static readonly DependencyProperty ValeurProperty =
DependencyProperty.Register("Valeur", typeof(int), typeof(NumericUpDown), new UIPropertyMetadata(0, onValeurPropertyChanged ));
private static void onValeurPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
}
public NumericUpDown()
{
InitializeComponent();
bind = new Binding();
bind.Source = this;
bind.Mode = BindingMode.TwoWay;
bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
bind.Path = new PropertyPath("Valeur");
this.txtNum.SetBinding(TextBox.TextProperty, bind);
}
private void btPlus_Click(object sender, RoutedEventArgs e)
{
if (this.Valeur < 42)
{
this.Valeur = this.Valeur + 1;
}
/*txtNum.Text = this.Valeur.ToString();*/
}
private void btMoins_Click(object sender, RoutedEventArgs e)
{
if (this.Valeur > 1)
{
this.Valeur = this.Valeur - 1;
}
/*txtNum.Text = this.Valeur.ToString();*/
}
private void txtNum_PreviewKeyDown(object sender, KeyEventArgs e)
{
Key tmp = e.Key;
if (!(tmp.ToString() == "D1" || tmp.ToString() == "NumPad1" ||
tmp.ToString() == "D2" || tmp.ToString() == "NumPad2" ||
tmp.ToString() == "D3" || tmp.ToString() == "NumPad3" ||
tmp.ToString() == "D4" || tmp.ToString() == "NumPad4" ||
tmp.ToString() == "D5" || tmp.ToString() == "NumPad5" ||
tmp.ToString() == "D6" || tmp.ToString() == "NumPad6" ||
tmp.ToString() == "D7" || tmp.ToString() == "NumPad7" ||
tmp.ToString() == "D8" || tmp.ToString() == "NumPad8" ||
tmp.ToString() == "D9" || tmp.ToString() == "NumPad9" ||
tmp.ToString() == "D0" || tmp.ToString() == "NumPad0" ||
tmp.ToString() == "Back" || tmp.ToString() == "Return" ||
tmp.ToString() == "Enter" || tmp.ToString() == "Tab"))
{
e.Handled = true;
}
}
private void txtNum_LostFocus(object sender, RoutedEventArgs e)
{
if (txtNum.Text == "" || txtNum.Text == " ")
{
txtNum.Text = "1";
}
}
}
} |
Code Xaml
------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
<UserControl x:Class="Verification_Lotto.NumericUpDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="24" Width="100">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox Name="txtNum" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" PreviewKeyDown="txtNum_PreviewKeyDown" LostFocus="txtNum_LostFocus"></TextBox>
<RepeatButton Name="btPlus" Grid.Column="1" Grid.Row="0" Content="+" FontSize="8" VerticalContentAlignment="Top" Click="btPlus_Click" Padding="0"></RepeatButton>
<RepeatButton Name="btMoins" Grid.Column="1" Grid.Row="1" Content="-" FontSize="8" VerticalContentAlignment="Top" Click="btMoins_Click" Padding="0"></RepeatButton>
</Grid>
</UserControl> |
Un grand merci.