wpf控件属性绑定的静态方法

        控件上内容的帮顶有分两种一种是数据绑定datacontext,还有一种是用属性绑定,本文用一个实例来说明属性绑定的方法,同时吐槽一下,vs对wpf的支持不太好,特别是编辑视图文件xaml时,很多错误编译后就正常了。

缩放前:

放大后效果:

比较两种图片,可以发现,第一个图形长宽都等比增加 ,第二张图是个正方形,第三张长度是宽度的两倍

先上设计视图:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="589.256">
    <Window.Resources  >
        <local:ScareConvert  x:Key="sc" />//将接口类的实现方法与sc绑定,实现第三张图的放大方法 
    </Window.Resources >
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions >

//第一个图形

        <Rectangle  
            RadiusX="20"
            RadiusY="20"
            Grid.Row="0"
            Margin="10"
            Fill="Green" />

//第二个图形
        <Rectangle
            Grid.Row="1"
            Margin="10"
            Width="{Binding   Path=ActualHeight  , RelativeSource={RelativeSource   Mode= Self}  }"//宽度等于实际的高度
            Fill="Green" />

//第三个图形,此处用border代替rectangle
        <Border      
            Grid.Row="2"
            Margin="10"
            Width="{Binding 
            Path=ActualHeight  ,
            RelativeSource={RelativeSource   Mode= Self} ,
            Converter={StaticResource sc},
            ConverterParameter=2
            }"
        
           Background="Green">
            <UniformGrid   Rows="2">
                <TextBlock Grid.Row="0" 
                           Text="{Binding Path=Width ,//这个值会随着图形的尺寸一起变化
                           RelativeSource={RelativeSource AncestorType= Border  }
                           }"
                           />
                <TextBlock Grid.Row="1"     
                           Text="{Binding Path=ActualHeight ,//这个值会随着图形的尺寸一起变化
                           RelativeSource={RelativeSource AncestorType= Border  }
                           }"
                           />
            </UniformGrid> 
         </Border>
    </Grid>
</Window>

 

需要增加一个接口实现类ScareConvert.cs

    class ScareConvert : IValueConverter
    {

//这个方法就是让出入的宽度乘2返回给高度
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double height = (double)value;
          if(  double.TryParse ( parameter.ToString(),out double  scale))
            {
                return height*scale;
            }
            else
            {
                throw new Exception();
            }
         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值