其他分享
首页 > 其他分享> > WPF模型绑定简单使用

WPF模型绑定简单使用

作者:互联网

开始

最开始想要把角度转换成弧度。
AngleToRadian.xaml

<Grid>
    <StackPanel>
        <DockPanel>
            <Label>角度</Label>
            <TextBox VerticalContentAlignment="Center"></TextBox>
        </DockPanel>
        <DockPanel>
            <Label>弧度</Label>
            <TextBox VerticalContentAlignment="Center"></TextBox>
        </DockPanel>
    </StackPanel>
</Grid>

添加模型

在模型中,实现INotifyPropertyChanged接口。在设置属性值的时候,通知更改。
AngleToRadianModel.cs

public class AngleToRadianModel : INotifyPropertyChanged
{
    private string angle;
    private string angle2;
    private string radian;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Angle
    {
        get => angle; set
        {
            angle = value;
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(Angle)));
        }
    }

    public string Angle2
    {
        get => angle2; set
        {
            angle2 = value;
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(Angle2)));
        }
    }

    public string Radian
    {
        get => radian; set
        {
            radian = value;
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(Radian)));
        }
    }

    public (bool result, string message) DoAngleToRadian()
    {
        if (!string.IsNullOrEmpty(angle))
        {
            if (!double.TryParse(angle, out var value))
            {
                var match = Regex.Match(angle, @"(\d+)°(\d+)′(\d+)″");
                if (match.Success)
                {
                    var a1 = int.Parse(match.Groups[1].Value);
                    var a2 = int.Parse(match.Groups[2].Value);
                    var a3 = double.Parse(match.Groups[3].Value);
                    value = a1 + a2 * 1d / 60 + a3 * 1 / (60 * 60);
                }
                else
                {
                    return (false, "角度格式不正确");
                }
                Angle2 = value.ToString();
            }
            Radian = ((Math.PI / 180) * value).ToString();
            return (true, "");
        }
        return (false, "角度格式不正确");
    }
}

最后

在xaml中,放置DataContext,绑定TextBox和模型上的属性。
添加点击事件。
AngleToRadian.xaml

<Page.DataContext>
    <local:AngleToRadianModel></local:AngleToRadianModel>
</Page.DataContext>
<Grid>
    <StackPanel>
        <DockPanel>
            <Label>角度</Label>
            <TextBox VerticalContentAlignment="Center" Text="{Binding Angle}"></TextBox>
        </DockPanel>
        <DockPanel>
            <Label>弧度</Label>
            <TextBox VerticalContentAlignment="Center" Text="{Binding Radian}"></TextBox>
        </DockPanel>
        <DockPanel>
            <Label>角度(小数)</Label>
            <TextBox VerticalContentAlignment="Center" Text="{Binding Angle2}"></TextBox>
        </DockPanel>
        <Button HorizontalAlignment="Left"  Padding="20 0" Click="Button_Click" >转换</Button>
    </StackPanel>
</Grid>

AngleToRadian.xaml.cs

public AngleToRadianModel Model => (AngleToRadianModel)DataContext;
private void Button_Click(object sender, RoutedEventArgs e)
{
    var result = Model.DoAngleToRadian();
    if (!result.result)
    {
        MessageBox.Show(result.message);
    }
}

标签:angle,string,模型,绑定,value,var,WPF,public,match
来源: https://www.cnblogs.com/naergaga/p/16327021.html