Archiv der Kategorie: C#

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

INotifyPropertyChanged – Class / Event / Property

Class

using System.ComponentModel;

public class MainViewModel : INotifyPropertyChanged
{
   // ...
}

INotifyPropertyChanged – Event

        #region INotifyChangedProperty

        public event PropertyChangedEventHandler PropertyChanged;
        
        // .NET 3.x
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        // .NET 4.x
        //private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        //{
        //    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        //}

        #endregion

Property

        private string _Property;
        public string Property
        {
            get
            {
                return _Property;
            }
            set
            {
                if (value != this._Property)
                {
                    this._Property = value;
                    NotifyPropertyChanged("Property");
                }
            }
        }

 

 

Restart as Admin

static void RestartAsAdmin()
{
    var startInfo = new ProcessStartInfo("yourApp.exe") { Verb = "runas" };
    Process.Start(startInfo);
    Environment.Exit(0);
}