ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WPF模型绑定简单使用

2022-05-30 16:04:37  阅读:57  来源: 互联网

标签:angle string 模型 绑定 value var WPF public match


开始

最开始想要把角度转换成弧度。
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

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有