none
[UWP][VB][XAML]Refreshing a ContentControl RRS feed

  • Question

  • I have a ContentControl. The DataTemplate used for the ContentTemplate contains an ItemsControl (and several other controls, of course), for which I of course have a DataTemplate as the ItemTemplate. The ItemsSource for the ItemsControl is bound to a property of the ContentControl's Content. When I update the Content of the ContentControl I of course want the change to be shown on the page. However, the change is not being displayed even though the Content is updated. How can I make the ContentControl show the update? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, June 17, 2018 2:12 AM

All replies

  • Hi Nathan Sokalski,

    >>” When I update the Content of the ContentControl I of course want the change to be shown on the page. However, the change is not being displayed even though the Content is updated. How can I make the ContentControl show the update?”

    Please check if you’ve implemented the INotifyPropertyChanged interface for your class. Since I did not see your code, It’s hard to help you diagnose your issue. You’d better post a simple reproducible code sample here.

    Anyway, please check my following code sample, it worked well on my side.

    <StackPanel>
            <ContentControl x:Name="contentControl">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding tests}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"></TextBlock>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>
    
            <Button Content="update" Click="Button_Click"></Button>
        </StackPanel>
    Public NotInheritable Partial Class MainPage
        Inherits Page
    
        Private Property tests As ObservableCollection(Of Test)
    
        Public Sub New()
            Me.InitializeComponent()
            tests = New ObservableCollection(Of Test)()
            tests.Add(New Test() With {
                .Name = "name1"
            })
            tests.Add(New Test() With {
                .Name = "name2"
            })
            contentControl.Content = New With {tests
            }
        End Sub
    
        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            tests(0).Name = "_name1"
        End Sub
    End Class
    Public Class Test
        Inherits INotifyPropertyChanged
    
        Public Event PropertyChanged As PropertyChangedEventHandler
    
        Private Sub RaisePropertyChanged(ByVal PropertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
        End Sub
    
        Private _Name As String
    
        Public Property Name As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
                RaisePropertyChanged("Name")
            End Set
        End Property
    End Class

    Best Regards,

    Xavier Xie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 18, 2018 1:34 AM