C#什么取得WPF中LISTBOX里面每个ITEM的值
时间: 2024-02-03 11:13:07 浏览: 33
你可以使用以下代码来获取ListBox中每个Item的值:
```csharp
foreach (var item in listBox.Items)
{
string value = item.ToString();
// Do something with the value
}
```
或者你可以使用以下代码来获取选中的Item的值:
```csharp
if (listBox.SelectedItem != null)
{
string value = listBox.SelectedItem.ToString();
// Do something with the selected value
}
```
相关问题
WPF的LISTBOX什么取得自己输入的值
WPF的ListBox控件是用来显示一组项的集合,而不是用来输入值的控件。如果你想让用户输入值,可以考虑使用WPF中的TextBox控件。当用户在TextBox中输入值后,可以使用TextBox的Text属性来获取用户输入的值。例如:
```
<TextBox x:Name="myTextBox" Width="120" />
```
```
string userInput = myTextBox.Text;
```
在上面的XAML代码中,定义了一个名为"myTextBox"的TextBox控件,当用户在其中输入值后,可以使用myTextBox的Text属性来获取用户输入的值,将其赋值给一个字符串类型的变量userInput中。
WPF的LISTBOX什么让他隔个条目显示不同颜色
要让WPF的ListBox隔个条目显示不同颜色,可以使用ListBox的ItemContainerStyle属性来自定义ListBoxItem的样式。在ListBoxItem的样式中,可以使用Trigger来设置不同的颜色。
下面是一个示例代码,可以让ListBox隔个条目显示不同颜色:
```
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightGray" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Items>
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
<ListBoxItem>Item 3</ListBoxItem>
<ListBoxItem>Item 4</ListBoxItem>
<ListBoxItem>Item 5</ListBoxItem>
</ListBox.Items>
</ListBox>
```
在上面的示例代码中,使用了ListBox的AlternationIndex属性来区分不同的ListBoxItem,然后使用Trigger来设置不同的Background颜色。这样就能让ListBox隔个条目显示不同颜色了。