I 
Command vengono utilizzati per evitare di assegnare un determinato comportamento direttamente al controllo e per riutilizzare semplicemente le funzionalità di un'applicazione.
Esempio su come utilizzarli:
C#
| public MainWindow()
{
    InitializeComponent();
}
 
private void Comando_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    //Condizione per determinare se eseguire o meno il comando
    e.CanExecute = textBox1.Text == "ok";
    e.Handled = true;
}
 
private void Comando_Execute(object sender, ExecutedRoutedEventArgs e)
{
    e.Handled = true;
    MessageBox.Show("Command in WPF");
} | 
 
XMAL
| <Window x:Class="TestWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Command in WPF" Height="184.831" Width="346.723">
    <Window.Resources>
        <RoutedUICommand x:Key="Comando" />
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="{StaticResource Comando}" CanExecute="Comando_CanExecute" Executed="Comando_Execute" />
    </Window.CommandBindings>
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,23,0,0" TextWrapping="Wrap" Name="textBox1" VerticalAlignment="Top" Width="120"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="149,23,0,0" VerticalAlignment="Top" Width="75" Command="{StaticResource Comando}"/>
    </Grid>
</Window> | 
 
Nessun commento:
Posta un commento