HandyOrg / HandyControl

Contains some simple and commonly used WPF controls
https://handyorg.github.io/
MIT License
6.18k stars 1.04k forks source link

PropertyGrid新增文件选择类型 #1503

Open benyuz opened 11 months ago

benyuz commented 11 months ago

Feature request type

sample request

Is your feature request related to a problem? Please describe

请求给PropertyGrid控件新增文件选择类型,具体表现类似: 有个文本框和选择按钮,点击后弹出文件/文件夹选择框,选择后文件路径显示在文本框里。

Describe the solution you'd like

看到官方文档有自定义方式,但是不会弄。如果做成标准就很好。https://handyorg.github.io/handycontrol/extend_controls/propertyGrid/

Describe alternatives you've considered

No response

Additional context

No response

Jotaro2025 commented 4 months ago

我实现了一个自定义

public class FilePropertyEditor : PropertyEditorBase
{
    public override FrameworkElement CreateElement(PropertyItem propertyItem)
    {
        Grid grid = new();
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); 

        TextBox textBox = new()
        {
            Margin = new Thickness(0, 0, 5, 0), 
            HorizontalAlignment = HorizontalAlignment.Stretch, 
        };

        Grid.SetColumn(textBox, 0); 

        Button openFolderButton = new()
        {
            Content = "...",
            Width = 35,
            HorizontalAlignment = HorizontalAlignment.Right
        };

        openFolderButton.Click += (sender, e) =>
        {
            Microsoft.Win32.OpenFileDialog op = new();
            if ((bool)op.ShowDialog())
            {
                propertyItem.Value.GetType().GetProperty(propertyItem.PropertyName.ToString()).SetValue(propertyItem.Value, op.FileName);
            }
        };
        Grid.SetColumn(openFolderButton, 1); 
        grid.Children.Add(textBox);
        grid.Children.Add(openFolderButton);

        return grid;
    }

    public override DependencyProperty GetDependencyProperty()
    {
        // 返回TextBox的TextProperty作为绑定的依赖属性
        return TextBox.TextProperty;
    }

    public override void CreateBinding(PropertyItem propertyItem, DependencyObject element)
    {
        if (element is Grid grid)
        {
            foreach (var item in grid.Children)
            {
                if (item is TextBox textBox)
                {
                    BindingOperations.SetBinding(textBox, GetDependencyProperty(), new Binding($"{propertyItem.PropertyName}")
                    {
                        Source = propertyItem.Value,
                        Mode = BindingMode.TwoWay,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                        Converter = GetConverter(propertyItem)
                    });
                    break;
                }
            }
        }
        else
        {
            // 如果element不是Grid,可以调用基类的实现或进行其他处理
            base.CreateBinding(propertyItem, element);
        }

    }
}

但是有一个问题是,绑定的属性必须实现通知接口,才能实现UI联动,不然选择了,但是TextBox的值未改变:

[Editor(typeof(FilePropertyEditor), typeof(PropertyEditorBase))]
public string Hyp
{
    set => SetProperty(ref hyp, value);
    get => hyp;
}
private string hyp;