WPF短绑定

时间:2014-12-28 22:20:08

标签: c# wpf

我真的不明白为什么我不能这样绑定:

<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); }
    }
}

1 个答案:

答案 0 :(得分:0)

没有人回答我的问题...... 但是很多你给我一个减号 - .-

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

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

所有孩子的绑定更改DataContext所以我不需要在所有的bingings中重写它