WPF Short binding

本文关键字:binding Short WPF | 更新日期: 2023-09-27 18:35:34

我真的不明白为什么我不能像它一样绑定:

<Label Content="{Binding SomeObiect}"/>

在很多时候看到类似的东西,但在我的程序中不起作用......为什么?:(

<UserControl x:Class="GAME___ala_Mario.View.Controls.SkillButton"
             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" 
             xmlns:Converers="clr-namespace:GAME___ala_Mario.View.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="btn_Skill">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20*"/>
            <RowDefinition Height="5*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="5*"/>
        </Grid.ColumnDefinitions>
        <!--Tło-->
        <Viewbox Grid.Column="0" Grid.Row="0"
                 Grid.ColumnSpan="2" Grid.RowSpan="2">
         <!-- AND I USE BINDING LIKE IT: (always) -->
            <Image Source="{Binding ElementName=btn_Skill, Path=BackgroundImageSource}"/>
        </Viewbox>
    </Grid>
</UserControl>

和代码隐藏:

public partial class SkillButton : UserControl
{
    [...]
    public static readonly DependencyProperty BackgroundImageSourceProperty = DependencyProperty.Register("BackgroundImageSource", typeof(ImageSource), typeof(SkillButton));
    public ImageSource BackgroundImageSource
    {
        get { return (ImageSource)GetValue(BackgroundImageSourceProperty); }
        set { SetValue(BackgroundImageSourceProperty, value); }
    }
}

WPF Short binding

没有人回答我的问题...但是你们中的许多人给我一个减-.-

我不知道这是否符合标准,但我确实喜欢它:

<Grid DataContext="{Binding ElementName=btn_Skill}">     
    <Viewbox>
        <Image Source="{Binding BackgroundImageSource}"/>
    </Viewbox>
</Grid>

该绑定更改了所有子项的数据上下文,因此我不需要在所有大猩猩中重写它