将属性绑定到DataTemplate

时间:2018-04-20 09:39:09

标签: wpf xaml binding datatemplate

我使用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

属性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

此对象的属性设置为CollapsedString.Empty

接下来,我更改此对象的值,如下所示:

    Public Sub ShowAlert()
        Me.AlertSummary.Message = "Test"
        Me.AlertSummary.Visibility = Visibility.Visible
        'Me.StartTimerAlert()
        RaisePropertyChanged("AlertSummary")
    End Sub

但它没有用。有两个问题:

  • 在开始时,Visibility设置为Collapsed时,Border可见。
  • 当我更改Message属性时,它不会在视觉上更新。

我认为Binding存在问题,但我不知道在哪里。我尝试了不同的东西,但总有这些问题。 此外,我直接在TextBlock下面的ContentControl以及Binding工作查找中绑定了该属性。

你有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您应该将数据模板更改为:

 <DataTemplate x:Key="AlertWarningMessage">
        <Grid>
            <Border Visibility="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=DataContext.Visibility}" Background="AliceBlue" HorizontalAlignment="Stretch" Height="30">
                <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="WARNING !" FontWeight="Bold" Foreground="Red" FontSize="13"/>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=DataContext.Message}" Foreground="Red" Margin="5,0,0,0"/>
                </WrapPanel>
            </Border>
        </Grid>
    </DataTemplate>

你的AlertInfos就此(它在C#上,所以试着把它翻译成VB)

public class AlertInfos
{
    private string message;

    public string Message
    {
        get
        {
            return this.message;
        }
        set
        {
            if (this.message != value)
            {
                this.message = value;
            }
        }
    }
    private Visibility visibility;

    public Visibility Visibility
    {
        get
        {
            return this.visibility;
        }
        set
        {
            if (this.visibility != value)
            {
                this.visibility = value;
            }
        }
    }
}

它应该有效,至少它在我的电脑上工作

答案 1 :(得分:0)

我不熟悉VB,但Message需要RaisePropertyChanged

可见性通常也会受到限制,这也是RaisePropertyChanged - 然后使用BooleanToVisibilityConverter

确保您的属性是公开的 - 具有私有支持变量和RaisePropertyChanged。

    private bool _isSomethingVisibile;
    public bool IsSomethingVisibile
    {
        get { return _isSomethingVisibile; }
        set
        {
            _isSomethingVisibile = value;
            RaisePropertyChanged();
        }
    }

您无需在绑定前添加隐含的“DataContext”。