Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何绑定到另一个UserControl中的UserControl属性?_Wpf_Vb.net_Xaml_User Controls - Fatal编程技术网

Wpf 如何绑定到另一个UserControl中的UserControl属性?

Wpf 如何绑定到另一个UserControl中的UserControl属性?,wpf,vb.net,xaml,user-controls,Wpf,Vb.net,Xaml,User Controls,我有一个简单的UserControl,显示图标和文本: <UserControl x:Class="IconLabel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformat

我有一个简单的
UserControl
,显示图标和文本:

<UserControl x:Class="IconLabel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="26" d:DesignWidth="200" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Image x:Name="imgIcon" Source="{Binding Path=IconPath}" Stretch="UniformToFill" Width="26" Height="26" Margin="3,0" />
        <Label Content="{Binding Path=LabelText}" Margin="5,0" Grid.Column="1" />
    </Grid>
</UserControl>
到目前为止效果不错。我可以在XAML中设置它的属性,它们得到了正确的使用:

<local:IconLabel LabelText="Test"/>
隐藏以下代码:

Public Class IconLabelProgress

    'These are just meant to be passed along to the IconLabel
    Public Property IconPath As String
        Get
            Return GetValue(IconPathProperty)
        End Get
        Set(ByVal value As String)
            SetValue(IconPathProperty, value)
        End Set
    End Property

    Public Shared ReadOnly IconPathProperty As DependencyProperty = DependencyProperty.Register("IconPath", GetType(String), GetType(IconLabelProgress), New PropertyMetadata(""))

    Public Property PropName As String
        Get
            Return GetValue(PropNameProperty)
        End Get
        Set(ByVal value As String)
            SetValue(PropNameProperty, value)
        End Set
    End Property

    Public Shared ReadOnly PropNameProperty As DependencyProperty = DependencyProperty.Register("PropName", GetType(String), GetType(IconLabelProgress), New PropertyMetadata("PropName"))

    'This one is new
    Public Property ActualValue As Double
        Get
            Return GetValue(ActualValueProperty)
        End Get
        Set(ByVal value As Double)
            SetValue(ActualValueProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ActualValueProperty As DependencyProperty = DependencyProperty.Register("ActualValue", GetType(Double), GetType(IconLabelProgress), New PropertyMetadata(0.0))
End Class
如果我现在尝试实例化此控件并为内部
IconLabel
控件的标签传入一个值,如下所示:

<local:IconLabelProgress x:Name="ilp1" PropName="Test" ActualValue="5.0" />

然后它将不会在标签上显示“Test”,而是返回到通过
PropertyMetadata(“LabelText”)
指定的默认值。但是,
实际值
使用正确

如何使外部控件将值传递给嵌套控件?

默认情况下(您没有指定任何其他内容),绑定是从对象
DataContext
解析的。因此,您的
IconLabel
在其
DataContext
上搜索名为
IconPath
的属性

要指定搜索属性的位置是外部控件,可以将
ElementName
添加到绑定中,并在
IconLabelProgress
上设置name属性,或者指定一个
RelativeSource
类似于中接受答案的第二个示例


希望它能有所帮助。

一般来说,不要像您对用户控件那样显式设置用户控件的
DataContext
属性

<UserControl x:Class="IconLabel" ...
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

这似乎对我不起作用。通过删除
DataContext
并直接设置
RelativeSource
IconLabel
在直接使用时甚至不会显示值(没有外部
IconLabelProgress
)。
标签的内容保持空白。对不起,我的错。将
relativesourceself
替换为
relativesourceancestortype=UserControl
。好的,这似乎是正确的,但还没有完全正确。我现在可以再次直接使用
IconLabel
,但我似乎仍然无法(或不知道如何)将
IconLabelProgress
中设置的值传递到内部
IconLabel
。它只显示DependencyProperty的默认值“LabelText”。[此外,我必须在AncestorType之外添加“Mode=FindAncestor”,否则会给我一个错误。]当然,您还应该从外部用户控件中删除显式DataContext,添加write它与RelativeSource的绑定,例如
啊,是的。我已经删除了
DataContext
并添加了单个
RelativeSource
条目,但还没有将它们设置为
FindAncestor
。似乎正在工作。:)
<local:IconLabelProgress x:Name="ilp1" PropName="Test" ActualValue="5.0" />
<UserControl x:Class="IconLabel" ...
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<local:IconLabel LabelText="{Binding Path=PropName}" ... />
<Label Content="{Binding Path=LabelText,
                 RelativeSource={RelativeSource AncestorType=UserControl}}" ... />