开发者

How to refactor models without breaking WPF views?

开发者 https://www.devze.com 2022-12-31 04:27 出处:网络
I\'ve just started learning WPF and like the power of databinding it presents; that is ignoring the complexity and confusion for a noob.

I've just started learning WPF and like the power of databinding it presents; that is ignoring the complexity and confusion for a noob.

My concern is how do you safely refactor your models/viewmodels without breaking the views that use them?

Take the following snippet of a view for example:

<Grid>
    <ListView ItemsSource="{Binding Contacts}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                <GridViewColumn Header="DOB" DisplayMemberBinding="{Binding Path=DateOfBirth}"/>
                <GridViewColumn Header="# Pets" DisplayMemberBinding="{Binding Path=NumberOfPets}"/>
                <GridViewColumn Header="Male" DisplayMemberBinding="{Binding Path=IsMale}"/>
            </GridView>

        </ListView.View>
    </ListView>
</Grid>

开发者_JAVA百科The list is bound to the Contacts property, IList(Of Contact), of the windows DataSource and each of the properties for a Contact is bound to a GridViewColumn.

Now if I change the name of the NumberOfPets property in the Contact model to PetCount the view will break. How do I prevent the view breaking?


Unfortunatly this is a known issue in WPF, the xaml is not taken into account when you refactor the entities behind the bindings. I have been making use of third party refactoring tools such as resharper or coderush to solve the problem, they tend to handle it better than the built in refactor function in visual studio.


If you really don't want to change the view (for whatever reasons) you could leave the NumberOfPets property in the original ViewModel and set its getter to the PetCount property of the refactored ViewModel, there fore this won't break the View.

0

精彩评论

暂无评论...
验证码 换一张
取 消