2013-09-15 2 views
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; 
    } 
} 
+0

Что '' LocalizedStrings' и LocalizedResources'? Отправьте код для этого. – PoweredByOrange

ответ

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; 
    } 
+0

Хорошо, он работает: ((ButtonImage) d) .txtButtonText.Text = (строка) e.NewValue; –

Смежные вопросы