Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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
C# 将子对象绑定到父对象_C#_Wpf_Xaml - Fatal编程技术网

C# 将子对象绑定到父对象

C# 将子对象绑定到父对象,c#,wpf,xaml,C#,Wpf,Xaml,我是WPF的新手,想知道如何在WPF中将孩子绑定到家长。更清楚地说:我有一个像 <Button x:Name="JobViewer" Margin="5" Background="Transparent" Content="{TemplateBinding Content }" ToolTip="{Binding Name, RelativeSource={RelativeSource Self}}"> <Button.ContentTemplate>

我是WPF的新手,想知道如何在WPF中将孩子绑定到家长。更清楚地说:我有一个像

<Button x:Name="JobViewer" Margin="5" Background="Transparent" Content="{TemplateBinding Content }" ToolTip="{Binding Name, RelativeSource={RelativeSource Self}}">
    <Button.ContentTemplate>
        <DataTemplate>
            <Canvas Background="Black">
                <Border CornerRadius="8" Height="100" Width="100" Canvas.Left="-52" Canvas.Top="-50"
                BorderThickness="0" BorderBrush="Black">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="3" Opacity=".6" ShadowDepth="2" />
                    </Border.Effect>
                    <Image Source="{Binding}" />
                </Border>
                <Border x:Name="Update" CornerRadius="20" Width="35" Height="38" Canvas.Left="26" Canvas.Top="-65" Visibility="{Binding ElementName=mainWindow, Path=JobViewerUpdateVisibility}"BorderBrush="White" BorderThickness="2" RenderTransformOrigin="1.346,-0.919">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="3" Opacity=".6" ShadowDepth="2" />
                    </Border.Effect>
                    <Image Source="{StaticResource StateCompletedIcon}"></Image>
                </Border>
            </Canvas>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>

现在在按钮的内容中,我想使用模板绑定,这样我可以在XAML窗口中使用此用户控件并设置按钮的内容属性,并且该图像将被设置为第一个图像的图像源?
如果有人有其他想法来实现它,请也分享。

您可以使用
相对资源={relativesourcetemplatedparent}
绑定到
按钮的
内容。在本例中,我对
按钮使用了简单的
样式
,而不是
用户控件


用法:

<Button Content="C:\Whatever.png"/>


结果:

<Button Content="pack://application:,,,/Whatever.png"/>