djk*_*jkp 6 c# data-binding wpf xaml
我是Windows应用程序开发的新手,并尝试实现这样的显示:
标签号码:1标签号码:2标签号码:3 //屏幕左侧结束
标签号码:4标签号码:5 ......等等.
有人喜欢这样:
我在Windows 10通用应用程序开发中这样做.
提前致谢.
我的Xaml代码:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind comment_tags}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我的c#代码:
public List<string> comments_tags = new List<string>();
public MainPage()
{
this.InitializeComponent();
for(int i =0; i < 20; i++)
{
comments_tags.Add("Tag no: " + i);
}
}
Run Code Online (Sandbox Code Playgroud)
我试过的新方法:
public List<Border> comment_tags = new List<Border>();
for (int i = 0; i < 20; i++)
{
Border b_temp = new Border();
b_temp.CornerRadius = new CornerRadius(3);
b_temp.Background = new SolidColorBrush(Colors.Aqua);
TextBlock t = new TextBlock();
t.Text = "Tag no: " + i;
t.Foreground = new SolidColorBrush(Colors.Aqua)
b_temp.Child = t;
comments_tags.Add(b_temp);
}
Run Code Online (Sandbox Code Playgroud)
您处理标签的方法在这里不正确,您不需要这里的文本框,您需要一个可以理解标签是什么以及如何处理它的控件。
或者一个最小的实现可以是
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="PowderBlue" CornerRadius="5" BorderThickness="2" Height="45" Margin="5" >
<TextBlock Text="{Binding}" VerticalAlignment="Center" Margin="5"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
后面的代码是
public MainWindow()
{
InitializeComponent();
Items = new[] {"ABC", "DEF"};
this.DataContext = this;
}
public string[] Items
{ get; set; }
Run Code Online (Sandbox Code Playgroud)