我可以在DataTemplate中更改textBlockZona的值吗? 我需要创建一个动态标题列表,如何在代码中更改myTEXT内容?
<Page.Resources>
<DataTemplate x:Key="HeadTemplate">
<Grid
Width="745" Height="61" HorizontalAlignment="Left" VerticalAlignment="Top" AllowDrop="True" Background="Transparent">
<Grid
x:Name="gridZona" Width="122" Height="36" Margin="0,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle x:Name="ZonaButton" Fill="#FF2994FF" />
<TextBlock
x:Name="textBlockZona" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" FontWeight="Bold" Foreground="White"
Text="MYTEXT"
TextAlignment="Center" TextWrapping="Wrap" d:LayoutOverrides="TopPosition, BottomPosition" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="ViewTemplate">
<local:PosItemViewer />
</DataTemplate>
</Page.Resources>
<ListView x:Name="RigheView" Grid.Row="1" Width="744" Height="867" Margin="10,203,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"
ContainerContentChanging="RigheView_ContainerContentChanging"
HeaderTemplate="{StaticResource HeadTemplate}"
IsItemClickEnabled="True"
ItemTemplate="{StaticResource ViewTemplate}"
SelectionMode="None" />
答案 0 :(得分:-1)
您应该将public private(set) var keyboardLabel : UILabel {
get {
let label = UILabel()
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}
set {
keyboardLabel = newValue
}
}
的{{1}}属性绑定到Text
中TextBlock
类型的属性,该属性已设置为T
的{{1}} IEnumerable<T>
:
ItemsSource
然后,您只需将此属性设置为您想要的任何字符串,并且ListView
自动更新,前提是类型<TextBlock x:Name="textBlockZona" Text="{Binding YourTextProperty}" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" FontWeight="Bold" Foreground="White" TextAlignment="Center" TextWrapping="Wrap" d:LayoutOverrides="TopPosition, BottomPosition" />
实现TextBlock
接口:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx。
这是执行此操作的唯一正确方法。您无法以编程方式修改已在XAML标记中定义的模板。
修改:T
中的INotifyPropertyChanged
应绑定到视图模型中的属性,即TextBlock
的{{1}}本身,同样的方式。