简体   繁体   中英

WPF - control's width bound to DependencyProperty - setting default value via PropertyMetadata to asterisk (*)

I noticed a warning in VisualStudio that my binding for the width of a control to '*' (asterisk) is not valid. I was using PropertyMetadata inside the DependencyProperty definition to define default-values in the Code-behind. Since they are of type string this does not seem to work.

My XAML-Code:

<TextBox Name="MyTextBox" Text="This is my cute little text." Width="{Binding ElementName=MyBox, Path=TextBoxWidth}" [...]/>

My Code-behind:

    public static readonly DependencyProperty MinCharsQueryProperty = DependencyProperty.Register(
        "MinCharsQuery", typeof(int), typeof(MyBoxControl), new PropertyMetadata(1));
    public static readonly DependencyProperty TextBoxWidthProperty = DependencyProperty.Register(
        "TextBoxWidth", typeof(string), typeof(MyBoxControl), new PropertyMetadata("*"));
    public static readonly DependencyProperty TextBoxHeightProperty = DependencyProperty.Register(
        "TextBoxHeight", typeof(string), typeof(MyBoxControl), new PropertyMetadata("26"));

May do I have to change my type from string to something else? I ask that because I found other questions where the answer yields to using new GridLength(80, GridUnitType.Star); but my control in this case isnt a grid.

Or others suggest to use 'Auto' instead in combination with horizontal-alignment stretch what I neither would like to use because of other side-effects.

When putting the asterisk ' ' directly into Widht=" " that of course works as you know. When using the code behind to set that property the output say it expects a double and not a string. So my way using the Code-Behind is missing some kind of pre-calculation which is achieved like creating the new GridLength and assigning it directly to the controls-property instead of using the default-value of the property-metadata.

1) I am afraid I cannto achieve this by using the PropertyMetadata? 2) When changning to a direct assignment, whats the equivalent to GridLenth of Width and Height of any control? I only find grid-related-questions on this.

Thanks.

Edit: Changing the DependencyProperty to type double leads to an exception that string is expected. Using "Auto", "double.NaN" or "Double.NaN" as string does not work. It fails converting it to double as it just fails using '*'. Console says to try to use a fallbackvalue if given.

Using a fallbackvalue inside the binding works neither with "*", "Auto", "double.NaN" or "Double.NaN" with the same error as above.

In case of no Default Value is give, the same error appears with the difference that the default-input is .

Looks like it simply is not possible to use DataBinding on Width-Fields if not using regular double-values.

Using stuff like '*' or 'Auto' is only possible to be used directly in the XAML. All other tries will cause errors in the console output.

If one want to use Databinding but not give any default one has to live with these warnings in the console output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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