Binding properties through DataTemplates and ContentControl

后端 未结 2 514
旧巷少年郎
旧巷少年郎 2020-12-20 09:02

I liked this answer, and it almost fit me.

But, how can I achieve this if my DataTemplate is in a external ResourceDictionary

相关标签:
2条回答
  • 2020-12-20 09:34

    The DataContext in the UserControl is your model object, so you can directly bind to its properties like this:

    Text="{Binding SomeProperty}"
    

    (If only a path is specified the binding is relative to the DataContext, note that in the answer you linked the intention was to have a TwoWay binding on the DataContext itself which was a primitive string, this cannot be done using a simple binding like {Binding .}, a property path targeting an actual property needs to be specified)

    0 讨论(0)
  • 2020-12-20 09:51

    Since the DataContext behind OperationView will be an object of type Operation, then you simply bind to whatever property on Operation you want

    <!-- DataContext will be model:Operation per your DataTemplate -->
    <UserControl x:Class="TryERP2.Cadastro.View.OperationView"
                 ... some hidden NS ... >
        <StackPanel>
            <Label Content="Id" />
            <TextBox Text="{Binding Id}" />
            <Label Content="Description" />
            <TextBox Text="{Binding Description}" />
        </StackPanel>
    </UserControl>
    
    0 讨论(0)
提交回复
热议问题