简体   繁体   English

F#默认值中的依赖属性不匹配

[英]Dependency Property in F# Default Value does not match

I am trying to convert a C# Dependency Property that limits the maximum length of text entered into a ComboBox to F#. 我正在尝试转换一个C#依赖属性,它将输入ComboBox的文本的最大长度限制为F#。 The program is a MVVM program that uses F# for the model and viewmodel, and C# for the view. 该程序是一个MVVM程序,它使用F#作为模型和视图模型,使用C#作为视图。 the working C# code is this: 工作的C#代码是这样的:

public class myComboBoxProperties
    {
        public static int GetMaxLength(DependencyObject obj)
        {
            return (int)obj.GetValue(MaxLengthProperty);
        }

        public static void SetMaxLength(DependencyObject obj, int value)
        {
            obj.SetValue(MaxLengthProperty, value);
        }

        // Using a DependencyProperty as the backing store for MaxLength. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxLengthProperty =
            DependencyProperty.RegisterAttached("MaxLength",
            typeof(int),
            typeof(myComboBoxProperties),
            new UIPropertyMetadata(OnMaxLengthChanged));

        private static void OnMaxLengthChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            if (obj is ComboBox)
            {
                ComboBox comboBox = (ComboBox)obj;

                comboBox.Loaded += (sender, e) =>
                {
                    TextBox textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

                    if (textBox != null)
                    {
                        textBox.SetValue(TextBox.MaxLengthProperty, args.NewValue);
                    }
                };
            }
        }
    }

The F# code is this: F#代码是这样的:

type myComboBoxProperties() =

    static let OnMaxLengthChanged  (myobj1 : DependencyObject, args : DependencyPropertyChangedEventArgs)  =

        let comboBox = myobj1 :?> ComboBox

        comboBox.Loaded.Subscribe (fun _ -> 
                                    let textBox : TextBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) :?> TextBox 
                                    match textBox with
                                    | null -> ()
                                    |_ -> textBox.SetValue(TextBox.MaxLengthProperty, args.NewValue))

    static let MaxLengthProperty = DependencyProperty.RegisterAttached("MaxLength", typeof<int>, typeof<myComboBoxProperties>, new UIPropertyMetadata(OnMaxLengthChanged))

    static member GetMaxLength (myobj : DependencyObject) = myobj.GetValue(MaxLengthProperty) :?> int

    static member SetMaxLength (myobj : DependencyObject, value : int) = myobj.SetValue(MaxLengthProperty, value)

The problem I am having is that the XAML error I get is: 我遇到的问题是我得到的XAML错误是:

Default value type does not match type of property MaxLength 默认值类型与属性MaxLength类型不匹配

What am I doing wrong? 我究竟做错了什么?

You could try this 你可以试试这个

open System.Windows
open System.Windows.Controls

type MyComboBoxProperties() =

  static let OnMaxLengthChanged  (myobj1 : DependencyObject) (args : DependencyPropertyChangedEventArgs) =

    let comboBox = myobj1 :?> ComboBox

    comboBox.Loaded.Add (
      fun _ -> 
        let textBox : TextBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) :?> TextBox 
        match textBox with
        | null -> ()
        |_ -> textBox.SetValue(TextBox.MaxLengthProperty, args.NewValue)
      )

  static let MaxLengthProperty = 
    DependencyProperty.RegisterAttached(
      "MaxLength", 
      typeof<int>, 
      typeof<MyComboBoxProperties>, 
      UIPropertyMetadata(0, PropertyChangedCallback OnMaxLengthChanged)
      )

  static member GetMaxLength (myobj : DependencyObject) = myobj.GetValue(MaxLengthProperty) :?> int

  static member SetMaxLength (myobj : DependencyObject, value : int) = myobj.SetValue(MaxLengthProperty, value)

The key difference to your code is this UIPropertyMetadata(0, PropertyChangedCallback OnMaxLengthChanged) that converts OnMaxLengthChanged into a PropertyChangedCallback . 与您的代码的主要区别在于UIPropertyMetadata(0, PropertyChangedCallback OnMaxLengthChanged)OnMaxLengthChanged转换为PropertyChangedCallback

But it feels odd to me that you subscribe to the .Loaded even each time the max value you change. 但是,即使每次更改最大值,您也会订阅.Loaded I suspect you only like to subscribe the first time? 我怀疑你只想第一次订阅?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM