【发布时间】:2018-09-30 23:49:55
【问题描述】:
我正在使用DataTemplate,它位于ResourceDictionary 文件中。
<DataTemplate x:Key="AlertWarningMessage">
<Grid>
<Border Visibility="{Binding DataContext.Visibility}" Background="{StaticResource ResourceKey=AlertWarningMessageBackground}" HorizontalAlignment="Stretch" Height="30">
<WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="WARNING !" FontWeight="Bold" Foreground="{StaticResource ResourceKey=AlertWarningMessageForeground}" FontSize="13"/>
<TextBlock Text="{Binding DataContext.Message}" Foreground="{StaticResource ResourceKey=AlertWarningMessageForeground}" Margin="5,0,0,0"/>
</WrapPanel>
</Border>
</Grid>
</DataTemplate>
我将这个字典合并到我的UserControl 中,我正在使用这个模板:
<ContentControl ContentTemplate="{StaticResource AlertWarningMessage}" Grid.Row="2" Margin="0,2,0,0" DataContext="{Binding AlertSummary, UpdateSourceTrigger=PropertyChanged}" />
在我的虚拟机中,我正在使用一个具有 2 个属性的类:
Public Class AlertInfos
Public Property Visibility As Visibility
Public Property Message As String
Public Sub New(p_visibility As Visibility, p_msg As String)
Me.Visibility = p_visibility
Me.Message = p_msg
End Sub
End Class
Property VM 作为我的类:
Private _alertSummary As AlertInfos
Public Property AlertSummary() As AlertInfos
Get
Return _alertSummary
End Get
Set(ByVal value As AlertInfos)
_alertSummary = value
RaisePropertyChanged("AlertSummary")
End Set
End Property
此对象的属性设置为Collapsed 和String.Empty
接下来,我改变这个对象的值,像这样:
Public Sub ShowAlert()
Me.AlertSummary.Message = "Test"
Me.AlertSummary.Visibility = Visibility.Visible
'Me.StartTimerAlert()
RaisePropertyChanged("AlertSummary")
End Sub
但它不起作用。有2个问题:
- 一开始,当
Visibility设置为Collapsed时,Border是可见的。 - 当我更改
Message属性时,它没有视觉更新。
我认为我的Binding 有问题,但我不知道在哪里。我尝试了不同的东西,但总是有这些问题。
此外,我已经将属性直接绑定在ContentControl 下方的TextBlock 和Binding 工作查找中。
你有什么想法吗?
【问题讨论】:
标签: wpf xaml binding datatemplate