在我的Windows应用商店应用中,我有一个CueAnswerButton用户控件。它包含一个Image控件,它绑定到视图模型中的{Binding NormalImage}
。但是我在CueAnswerButton中有其他控件和属性,只要绑定属性发生变化就需要更新(我想在CueAnswerButton中更改时执行代码)。
我该怎么办?
我发现other articles解释了类似的东西,但并不完全。也许我不知道用于找到答案的正确术语。
感谢您的帮助。
// 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;
}
}
}
答案 0 :(得分:1)
您可以添加每次Dependency Property更改值时调用的委托......
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;
}
})
);
答案 1 :(得分:0)
给Jeff Brand +1,因为他的答案帮助我找到了解决方案。但是,解决此问题的最佳方法是停止尝试对抗系统并将updateSkinnableProperties()逻辑移动到ViewModel本身,并让它实现INotifyPropertyChanged。它暴露的一些属性更多以UI为中心,而不是以数据为中心(如画笔和厚度属性),但最终这是最干净,最好的解决方案。
谢谢!