1

在我的 Windows 应用商店应用程序中,我有一个 CueAnswerButton 用户控件。它包含一个绑定到{Binding NormalImage}视图模型中的图像控件。但是,我在 CueAnswerButton 中有其他控件和属性,只要绑定的属性更改,就需要更新它们(我想在 CueAnswerButton 更改时执行代码)。

我该怎么做?

我发现其他文章解释了类似的事情,但不完全是。也许我不知道用来找到答案的正确术语。

谢谢你的帮助。

// View data model for a cue answer button.

public class CueAnswerViewData : DependencyObject
{
  public CueAnswerViewData()
  {
  }

  public const string NormalImagePropertyTag = "NormalImageProperty";
  public static readonly DependencyProperty NormalImageProperty =
     DependencyProperty.Register(
         NormalImagePropertyTag,
         typeof(ImageSource),
         typeof(CueAnswerViewData),
         new PropertyMetadata(null));
  public ImageSource NormalImage
  {
    get
    {
      return (ImageSource)GetValue(
        NormalImageProperty);
    }
    set
    {
      SetValue(NormalImageProperty, value);
    }
  }

和 CueAnswerButton.xaml:

  <Grid>
    <Button x:Name="m_button" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" 
            BorderThickness="0"
            />

    <Image x:Name="m_image" 
           HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" 
           Stretch="Fill"
           Source="{Binding NormalImage}" 
           />

    ...

基本上,每当 ViewData 上的 NormalImage 属性发生变化时,我都想调用 CueAnswerButton.updateSkinnableProperties() 。是的,这是 wp7 应用程序的一个端口 :),它仍在进行中。

private void updateSkinnableProperties()
{
  bool skinned = isSkinned();

  m_image.IsHitTestVisible = skinned;
  m_button.IsHitTestVisible = !skinned;

  Background = m_correctHint ? m_correctAnswerGreenBrush : null;
  m_textbox.Background = m_correctHint ? m_correctAnswerGreenBrush : null;
  m_button.Background = m_correctHint ? m_correctAnswerGreenBrush : 
    m_phoneBackgroundBrush;

  m_button.BorderThickness = (skinned || m_correctHint) ?
    m_zeroThickness : m_phoneBorderThickness;

  m_textbox.Foreground = (skinned || m_correctHint) ?
    m_blackBrush : m_phoneForegroundBrush;

  m_button.Padding = skinned ?
    m_zeroThickness : m_phoneTouchTargetOverhang;

  if (!skinned)
  {
    m_button.Content = null;
  }

  updateSkinnableStateBasedProperties();
}

public bool isSkinned()
{
  bool skinned = false;

  if (m_normalImage != null && m_normalImage.ToString().Length > 0)
  {
    skinned = true;
  }

  return skinned;
}

private void updateSkinnableStateBasedProperties()
{
  if (!m_correctHint && isSkinned())
  {
    m_image.Visibility = Visibility.Visible;

    if (m_pressed && m_pressedImage != null)
    {
      m_image.Source = m_pressedImage;
    }
    else
    {
      m_image.Source = m_normalImage;
    }
  }
  else
  {
    m_image.Source = null;

    if(!m_correctHint)
    {
      m_textbox.Foreground = m_pressed ?
        m_phoneBackgroundBrush : m_phoneForegroundBrush;

      m_button.Background = m_pressed ?
        m_phoneForegroundBrush : m_phoneBackgroundBrush;
    }
  }
}
4

2 回答 2

1

您可以添加一个每次 DependecyProperty 更改值时都会调用的委托...

  public static readonly DependencyProperty NormalImageProperty =
     DependencyProperty.Register(
         NormalImagePropertyTag,
         typeof(ImageSource),
         typeof(CueAnswerViewData),
         new PropertyMetadata(null, (s, e) =>
                var ctrl = s as CueAnswerViewData;
                if (ctrl != null) {
                   ctrl.somePropertyToUpdate = 123;
                }
            })
            );
于 2013-01-04T21:07:34.377 回答
0

给 Jeff Brand +1,因为他的回答帮助我找到了解决方案。然而,解决这个问题的最好方法是停止尝试与系统对抗,将 updateSkinnableProperties() 逻辑移动到 ViewModel 本身,并让它实现 INotifyPropertyChanged。它公开的一些属性更加以 UI 为中心,而不是以数据为中心(例如 Brush 和 Thickness 属性),但最终这是最干净和最好的解决方案。

谢谢!

于 2013-01-08T00:09:22.753 回答