0

I'm using a DataTemplate which is inside a ResourceDictionary file.

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

I merge this dictionnary in my UserControl, and i'm using this template like this :

<ContentControl ContentTemplate="{StaticResource AlertWarningMessage}" Grid.Row="2" Margin="0,2,0,0" DataContext="{Binding AlertSummary, UpdateSourceTrigger=PropertyChanged}" />

In my VM, i'm using a class which have 2 properties :

    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 as my class :

    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

Properties of this object are set to Collapsed and String.Empty

Next, I change the values of this object, like this :

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

But it's not working. There are 2 problems :

  • At the begining, when the Visibility is set to Collapsed, the Border is visible.
  • When I change the Message property, it is not visually updated.

I think there is a problem with my Binding, but I don't know where. I tried differents things, but there is always these problems. Furthermore, I had bind the property directly in a TextBlock below the ContentControl, and the Binding working find.

Do you have any idea ?

2 Answers 2

1

You should change your data template to this:

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

And your AlertInfos to this (it's on C# so try to translate it to 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;
            }
        }
    }
}

It should work, at least it's working on my PC

Sign up to request clarification or add additional context in comments.

3 Comments

So... I added the RelativeSource and Path => it was better but not working at 100% (The Get was reached just one time (at the begining), but not after the update). Now, I added Inherits GalaSoft.MvvmLight.ViewModelBase to my class (and RaisePropertyChanged by property), and, Grid of my DataTemplate has DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ContentControl}}", and the binding targeted only the property name (Visibility or Message). Thank you very much !
That’s why I asked you if you had INotifyPropertyChanged in my other answer. You should raise it every time it changes. Galas I helped you :) pls upvote my answer so you help me too:)
After you'r first question, I tried to Implements INotifyPropertyChanged to my class, but it was not working. I think this is because the RelativeSource was missing. I have already upvoted your answer, but because I have only 3 of Reputation, it is not yet accounting (15 is needed) :/
0

I'm not familiar with VB but Message needs to RaisePropertyChanged

Visibilities are commonly bound too bools which also RaisePropertyChanged - these then use a BooleanToVisibilityConverter

Make sure your properties are public - have private backing variables and RaisePropertyChanged.

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

You don't need to preface your bindings with "DataContext" that is implied.

4 Comments

You are saying that I should to use 2 properties instead of my object ? And so with the RaisePropertyChanged, the binding will works ?
Your datacontext should be the object which holds your data. The data that you want to display on that object needs to be a public property with the gets sets notifying.
I tried to make an inheritance to my class but the binding not working again. I tried another way : 2 properties in my VM (Visibility and Message), and bind the DataTemplate components to these 2 properties. It's not working. But If I do that directly in my UserControl, it's OK. So the problem is when i'm using the Template, but I don't know why.
Check your output window to make sure you don't have any binding errors. Post how you are binding your UI to you object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.