WPF使用Thumb实现拖动和改变控件大小-2
  JWGppPCsR1pT 2023年11月02日 96 0

本章在WPF使用Thumb实现拖动和改变控件大小-1的基础上做修改

Selected属性

控件应该是被选中之后再显示ResizeThumb

public bool Selected
{
      get { return (bool)GetValue(SelectedProperty); }
      set { SetValue(SelectedProperty, value); }
}

// Using a DependencyProperty as the backing store for SelectedProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedProperty =
            DependencyProperty.Register("Selected", typeof(bool), typeof(MoveResizeItem), new PropertyMetadata(false));

Selected为依赖属性,因为在WPF中必须是依赖属性才能作为绑定源。

<ContentControl.Resources>
     <cvt:BoolVisualConvertion x:Key="BOOL_VISUAL_CVT"/>
 </ContentControl.Resources> 
<Grid Visibility="{Binding Selected, Converter={StaticResource BOOL_VISUAL_CVT}}">
   ...
</Grid>

用Grid将8个ReszeThumb包裹,否则Selected的绑定要写8次...

转换器BoolVisualConvertion定义如下:

namespace MoveResizeItem.Converter
{
    internal class BoolVisualConvertion : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? b = value as bool?;
            if (b == null)
            {
                return Visibility.Hidden;
            }
            return b == true ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility? v = value as Visibility?;
            v = v == null ? Visibility.Hidden : Visibility.Visible;
            return v;
        }
    }
}


代码下载

码云:  https://gitee.com/luan-it/move-resize-able.git

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
JWGppPCsR1pT