English 中文(简体)
使用对大型软件项目具有约束力的数据的明智方法。
原标题:A sensible way to use data binding for a large software project.

I am looking into the logistics of adding several WPF features to a 1,000,000+ line code-base. I am looking into using databinding and DataTemplates in XAML to easily display various objects. I am able to bind an observable collection: points to a ListBox and if I don t use a template, I can just provided a meaningful toString for the list items and that gets me somewhere. The problem comes in when I want to use a template to display various properties of the list items. It is my understanding that they all have to be DependencyProperties. parent is just a property. It looks like adding dependency properties is fairly complicated. All the classes you want to display have to inherit from DependencyObject and you have to set a bunch of stuff for each property you want to use. This does not seem feasible for a large codebase, to have to do all this for each property. I m wondering how databinding is used in practice, do you really add crap-tons of dependency properties to all of your classes or is there a better way?

谢谢!

    <ListBox Name="listBox1" 
             ItemsSource="{Binding Source={StaticResource points}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="Point!"/>
                    <TextBlock Text="{Binding parent}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
最佳回答

当我想使用模板来显示列表项目的各种属性时, 问题就会出现。 据我了解, 它们都必须是依赖性属性。 父属性只是一个属性。 看起来添加依赖性属性相当复杂。 您想要显示的所有类都必须从依赖性Object 中继承下来, 您必须为您想要使用的每个属性设置一堆东西 。

这不是真的。

您需要使用从属属性 < em> 或 < / em >, 您需要类型执行 < code> I notify property Changed , 但只有在您想要强制双向工作时才需要这样做。 这一要求存在, 以便当属性值改变时, WPF 得到通知, 而 UI 可以自动更新。 但是, 如果显示屏幕时属性值没有变化, 则没有要求( 除了属性值而不是字段值之外)。

如果您在对象内重新显示值,且在显示时不会改变,标准属性将正常工作。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签