mercredi 20 janvier 2016

WPF CheckBox IsChecked Property binding issues

Please help, I was trying to do this small example. My aim is to when I keep the checkbox ticked the app should show the the Ip address of the Host I enter. But The checkbox IsChecked property is never updated in the view model, Even it is been changed in the UI

My View `

    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.Background>
        <LinearGradientBrush>
            <LinearGradientBrush.GradientStops>
                <GradientStop Offset="0.00" Color="LavenderBlush" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
    </Grid.Background>
    <StackPanel Grid.Row="0" Margin="150,30,69,236" Grid.ColumnSpan="2">
        <TextBox x:Name="inputBox" Text="{Binding TxtHostName, Mode=TwoWay}"  Foreground="Azure" Background="YellowGreen" VerticalAlignment="Bottom" Height="45"/>

    </StackPanel>
    <Button Command="{Binding StartCommand }" Content="Get IP" HorizontalAlignment="Left" Margin="257,89,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.013,-0.273"/>
    <TextBlock Text="{Binding IpAddress}" Background="BlueViolet" Margin="150,153,69,104" Grid.ColumnSpan="2"  />
    <Button Content="Close" Command="{Binding CloseCommand}" HorizontalAlignment="Left" Margin="257,250,0,0" VerticalAlignment="Top" Width="75"/>
    <CheckBox Content="CheckBox" IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" Margin="150,111,0,0" VerticalAlignment="Top"/>
</Grid>

`

My ViewModel:

public class ViewModel:INotifyPropertyChanged
{
    #region INPC
    public void RaisePropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    #endregion

    private string txtHostName;

    public string TxtHostName
    {
        get { return txtHostName; }
        set { txtHostName = value;
        RaisePropertyChanged("TxtHostName");
        }
    }

    private string ipAddress;

    public string IpAddress
    {
        get { return ipAddress; }
        set { ipAddress = value;
        RaisePropertyChanged("IpAddress");
        }
    }


    private bool checkbox;

    public bool CheckBox
    {
        get { return checkbox; }
        set { checkbox = value;
        RaisePropertyChanged("IsSelected");
        }
    }

    public event EventHandler RequestClose;

    protected void OnRequestClose()
    {
        if (RequestClose != null)
            RequestClose(this, EventArgs.Empty);
    }

    private RelayCommand _StartCommand;
    public ICommand StartCommand
    {
        get
        {
            if (this._StartCommand == null)
                this._StartCommand = new RelayCommand(StartClick);
            return this._StartCommand;
        }
    }

    private RelayCommand _CloseCommand;
    public ICommand CloseCommand
    {
        get
        {
            if(this._CloseCommand==null)
                this._CloseCommand=new RelayCommand(CloseClick);
            return this._CloseCommand;
        }
    }

    private void CloseClick(object obj)
    {
        OnRequestClose();
    }

    private void StartClick(object obj)
    {
        if (checkbox)
        {
            string HostName = TxtHostName;
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);

            foreach (IPAddress ipaddr in ipaddress)
            {
                IpAddress = ipaddr.ToString();
            }

        }

        else
        {
            IpAddress = "Please tick the checkbox";
        }
    }
}

}

The RealyCommand is as it should be. The CheckBox Property value never changes weather I change it in the UI or not.

Aucun commentaire:

Enregistrer un commentaire