Archiv der Kategorie: Language

XAML / DataBinding / RelayCommand / ItemTemplate

XAML

<ListBox x:Name="KommZonenListBox" Grid.Row="1" Grid.ColumnSpan="2"  HorizontalContentAlignment="Stretch" 
            BorderBrush="{x:Null}" Background="LightGray" 
            ItemsSource="{Binding KommZonenListe}" 
            Margin="4" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding KommId}" Margin="4"/>
                <TextBlock Text="{Binding KommName}" Grid.Column="1" Margin="4"/>
                <TextBlock Text="{Binding KommName}" Grid.Column="2" Margin="4"/>
                <TextBlock Text="{Binding KommName}" Grid.Column="3" Margin="4"/>

                <Button Content="Delete" Margin="0,5,0,0" Command="{Binding DataContext.DeleteKommZoneCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ViewModel

private bool _DeleteKommZoneCommandCanExecute;
public RelayCommand<KommZone> _DeleteKommZoneCommand;
public RelayCommand<KommZone> DeleteKommZoneCommand
{
    get
    {
        return _DeleteKommZoneCommand ?? (_DeleteKommZoneCommand = new RelayCommand<KommZone>((kommzone) =>
                {
                    if (_DeleteKommZoneCommandCanExecute)
                    {
                        return;
                    }

                    _DeleteKommZoneCommandCanExecute = false;
                    DeleteKommZoneCommand.RaiseCanExecuteChanged();

                    ///
                    var KommZoneDelete = new KommZonenDeleteWindow(kommzone);
                    KommZoneDelete.ShowDialog();

                    _DeleteKommZoneCommandCanExecute = true;
                    DeleteKommZoneCommand.RaiseCanExecuteChanged();

                },
                (KommZone) => !_DeleteKommZoneCommandCanExecute));
    }
}

RelayCommand – RelayCommandGeneric

Get it from here: MVVM Light on Codeplex