在我的 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;
}
}
}