简体   繁体   English

XAML绑定到自定义控件

[英]XAML Binding to custom control

I have built a custom control and want to know how to properly bind in XAML to a property of an item in an Observable collection in the custom control. 我已经构建了一个自定义控件,并且想知道如何在XAML中正确绑定到该自定义控件中Observable集合中某个项目的属性。 The custom control property looks like this. 自定义控件属性如下所示。

Public Property MyPoints As ObservableDependencyObjectCollection(Of MyPoint)
    Get
        Return CType(GetValue(MyPointsProperty), ObservableDependencyObjectCollection(Of MyPoint))
    End Get
    Set(value As ObservableDependencyObjectCollection(Of MyPoint))
        SetValue(MyPointsProperty, value)
    End Set
End Property

MyPoint contains two properties X and Y MyPoint包含两个属性X和Y

Full XAML 完整的XAML

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfDependencyPropertyVB">

<Grid>
    <my:CustomControl1   HorizontalAlignment="Left" Margin="322,212,0,0" x:Name="CustomControl11" VerticalAlignment="Top" Width="145" Height="67">
        <my:CustomControl1.MyPoints>
            <my:MyPoint X="100" Y="{Binding Value, ElementName=Slider1}" />
        </my:CustomControl1.MyPoints>
    </my:CustomControl1>
    <Slider Height="26" HorizontalAlignment="Left" Margin="23,174,0,0" Name="Slider1" VerticalAlignment="Top" Width="273" />
    <Label Content="{Binding Value, ElementName=Slider1}" Height="41" HorizontalAlignment="Left" Margin="329,145,0,0" Name="Label1" VerticalAlignment="Top" Width="135" />
</Grid>

enter code here 在这里输入代码

In My XAML the set up looks like this: 在My XAML中,设置如下所示:

<my:CustomControl1 x:Name="CustomControl11" >
    <my:CustomControl1.MyPoints>
        <my:MyPoint X="100" Y="{Binding Value, ElementName=Slider1}" />
    </my:CustomControl1.MyPoints>
</my:CustomControl1>

If I set a break point in my control I can see that X=100 but i don't see that Y is updated when the Slider Value changes. 如果在控件中设置断点,则可以看到X = 100,但是当滑块值更改时,我看不到Y更新。

Any help would be greatly appreciated. 任何帮助将不胜感激。

My solution would be to add a dependency property ThePoint to CustomControl1 and then bind the slider value to ThePoint.Y . 我的解决方案是将一个依赖属性ThePoint添加到CustomControl1 ,然后将滑块值绑定到ThePoint.Y I'm assuming that MyPoint derives from DependencyObject and that X and Y are dependency properties. 我假设MyPoint派生自DependencyObject,并且X和Y是依赖项属性。

<my:CustomControl1 x:Name="theControl" />

<Slider Grid.Row="1" Value="{Binding ElementName=theControl, 
        Path=ThePoint.Y, Mode=TwoWay}"/>

You could then add PropertyChangedCallback handlers as required to add items to the collection (if that's what's required). 然后,您可以根据需要添加PropertyChangedCallback处理程序,以将项目添加到集合中(如果需要的话)。

Alternatively you can bind to an item in the collection: 或者,您可以绑定到集合中的项目:

<local:MyCustomControl x:Name="theControl" />

<Slider Grid.Row="1" Value="{Binding ElementName=theControl, 
        Path=MyPoints[0].Y, Mode=TwoWay}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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