4

I am working on some fairly large WPF project with a ton of classes and XAML design files.

But one thing is driving me nuts: The IntelliSense Binding autocompletion sometimes does not display the correct values (mostly on situations where i cannot provide the proper DataType and nothing baked is being used like eg. the Page content type)

Thus the actual question is: Is there some way to enforce IntelliSense to use a certain type for the autocompletion?

as random example, take this XAML:

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              DataType="{x:Type Accounting}">
    <ListView ItemsSource="{Binding Payments}">
        <ListView.View>
            <GridView>
                <!--
                    Auto completion still assumes the type is Accounting
                    and displays the properties of Accounting instead of
                    the required Payments.
                -->
                <GridViewColumn DisplayMemberBinding="{Binding Bank}"/>
            </GridView>
        </ListView.View>
    </ListView>
</DataTemplate>

And this for the C# Classes:

public class Accounting
{
    public List<Payment> Payments { get; set; }
}

public class Payment
{
    public string Bank { get; set; }
}
2
  • You can use the form {Binding Path=(xmlNameSpace:TypeName.PropertyName)} to force a type and get completion on PropertyName. May 16, 2019 at 11:47
  • Hi X39, any update for this issue? If Bradley's reply is helpful to resolve your issue. You could consider marking it as answer.If it persists, please check if the same issue exists after create a simple wpf project with similar structure.
    – LoLance
    May 30, 2019 at 7:48

1 Answer 1

3

You can use the form {Binding Path=(xmlNameSpace:TypeName.PropertyName)} to force a type and get completion on PropertyName.

This causes the Binding to treat the path as an attached property, which falls back to a "regular" property when the attached property type is the same as the bound type. I'm not sure if there is any additional overhead from trying to resolve it as an attached property or not, but it is enough for Visual Studio to start giving you auto-completion on the properties as you type. I consider it a bit of a hack, as it is definitely not the intended use of this syntax,

In your specific example it would look something like (adjust for your namespace):

<GridViewColumn DisplayMemberBinding="{Binding Path=(local:Payment.Bank)}"/>

enter image description here

5
  • This works to some degree, i at least now get the typos properly marked as error however, no autocompletion sadly :(
    – X39
    May 16, 2019 at 11:58
  • 1
    Odd, I'm getting a real autocomplete menu when I use this pattern (screenshot added). May 16, 2019 at 12:01
  • only now getting the proper type displayed on mousehover + the underlining on typos etc. so ... something is working
    – X39
    May 16, 2019 at 12:03
  • Just as a subtile note: this works up to some degree, there are some issues with this which means .. in the end one still has to remove the "abuse" of this to make it work properly sometimes. However, it does what was requested: allowing autocomplete in most cases (again, had classes where this still would not work ... cannot really tell why it sometimes did and sometimes did not work ... no common scheme)
    – X39
    Jun 3, 2019 at 9:17
  • Another late but very, very important note: the Path= is mandatory for this to work properly
    – X39
    Dec 23, 2021 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.