0

我有一个带有图像和文字的自制按钮。

<ButtonImageApp:ButtonImage   
BText="Button" 

这工作正常。但是当我尝试绑定时,我的按钮代码被破坏了。

这行不通。

BText="{Binding Path=LocalizedResources.PlayButton, Source={StaticResource LocalizedStrings}}"

XAML

<Button x:Class="ButtonImageApp.ButtonImage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        IsEnabledChanged="ButtonIsEnabledChanged"
        MouseEnter="ButtonMouseEnter"
        MouseLeave="ButtonMouseLeave">

    <Grid>
        <Image Stretch="None"
           HorizontalAlignment="Center"
           VerticalAlignment="Center"
               Grid.Row="0" Grid.Column="0"
           x:Name="image" />
        <TextBlock x:Name="txtButtonText"  
            Foreground="Black"             
            Text="{Binding Path=BText}"
            Grid.Row="0" Grid.Column="0" 
            Margin="20,51,0,-51" TextAlignment="Center"></TextBlock>
    </Grid>
</Button>

编码:

public static readonly DependencyProperty ButtonText = DependencyProperty.Register("BText", typeof(string), typeof(ButtonImage), null);
public string BText
{
    get { return (string)GetValue(ButtonText); }

    set
    {
        SetValue(ButtonText, value);
        txtButtonText.Text = value;
    }
}
4

1 回答 1

0

问题是,当使用绑定时,不会调用属性的设置器。
相反,您需要注册更改的依赖属性:

public static readonly DependencyProperty ButtonText = DependencyProperty.Register("BText", typeof(string), typeof(ButtonImage), new PropertyMetadata(ButtonTextChanged));

    private static void ButtonTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        txtButtonText.Text = e.NewValue;
    }
于 2013-09-15T17:48:37.963 回答