[UWP] [VB] [XAML]刷新ContentControl [英] [UWP][VB][XAML]Refreshing a ContentControl

查看:65
本文介绍了[UWP] [VB] [XAML]刷新ContentControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ContentControl。用于ContentTemplate的DataTemplate包含ItemsControl(当然还有其他几个控件),我当然有一个DataTemplate作为ItemTemplate。 ItemsControl的ItemsSource绑定到ContentControl内容的属性
。当我更新ContentControl的内容时,我当然希望更改显示在页面上。但是,即使更新了内容,也不会显示更改。如何让ContentControl显示
更新?谢谢。



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

解决方案

< blockquote>

Nathan Sokalski,


>>"当我更新ContentControl的内容时,我当然希望更改显示在页。但是,即使更新了内容,也不会显示更改。如何让ContentControl显示更新?"


请检查您是否为您的类实现了INotifyPropertyChanged接口。由于我没有看到您的代码,因此很难帮助您诊断问题。您最好在此处发布一个简单的可重现代码示例。


无论如何,请检查我的以下代码示例,它在我身边运作良好。

< 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"点击= QUOT; Button_Click">< /按钮>
< / StackPanel>


 Public NotInheritable Partial Class MainPage 
Inherits Page

私有属性测试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"
结束次级
结束类


公共类测试
继承INotifyPropertyChanged

公共事件PropertyChanged As PropertyChangedEventHandler

Private Sub RaisePropertyChanged(ByVal PropertyName As String)
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(PropertyName))
End Sub

Private _Name As String

公共属性名称字符串
获取
返回_Name
结束获取
设置(ByVal值为字符串)
_Name = value
RaisePropertyChanged(" Name")
End Set
End Property
End Class

最好的问候,


Xavier Xie


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/

解决方案

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


这篇关于[UWP] [VB] [XAML]刷新ContentControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆