返回 system.windows.controls.listboxitem 的多选列表框项目:带有 listboxitem 值
Multiselect listbox item returning system.windows.controls.listboxitem: with the listboxitem value
已选择的列表框项目 return system.windows.controls.listboxitem: ExampleValue.
private void Trade_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TradesSelected.Text = "";
foreach (object trade in Trade.SelectedItems)
{
TradesSelected.Text += (TradesSelected.Text == "" ? "" : ",") + trade.ToString();
}
}
如何删除 system.windows.controls.listboxitem: 部分,使其只显示 ExampleValue?
<StackPanel>
<TextBox x:Name="TradesSelected" Width="300" Padding="2" ></TextBox>
<ListBox SelectionMode="Multiple" x:Name="Trade" Width="300" Height="100" Padding="2" SelectionChanged="Trade_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>ExampleOne</ListBoxItem>
<ListBoxItem>ExampleTwo</ListBoxItem>
<ListBoxItem>ExampleThree</ListBoxItem>
</ListBox>
</StackPanel>
您需要将 trade
对象转换为它实际的类型。 object
类型的 .ToString()
方法只是 returns 类型名称。参见 here。
此外,交易对象的真实类型可能有一个 property/field/method,即 returns 您实际想要显示的字符串值。
TradesSelected.Text += (TradesSelected.Text == "" ? "" : ",") + (trade as ListBoxItem).Content.ToString();
已选择的列表框项目 return system.windows.controls.listboxitem: ExampleValue.
private void Trade_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TradesSelected.Text = "";
foreach (object trade in Trade.SelectedItems)
{
TradesSelected.Text += (TradesSelected.Text == "" ? "" : ",") + trade.ToString();
}
}
如何删除 system.windows.controls.listboxitem: 部分,使其只显示 ExampleValue?
<StackPanel>
<TextBox x:Name="TradesSelected" Width="300" Padding="2" ></TextBox>
<ListBox SelectionMode="Multiple" x:Name="Trade" Width="300" Height="100" Padding="2" SelectionChanged="Trade_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>ExampleOne</ListBoxItem>
<ListBoxItem>ExampleTwo</ListBoxItem>
<ListBoxItem>ExampleThree</ListBoxItem>
</ListBox>
</StackPanel>
您需要将 trade
对象转换为它实际的类型。 object
类型的 .ToString()
方法只是 returns 类型名称。参见 here。
此外,交易对象的真实类型可能有一个 property/field/method,即 returns 您实际想要显示的字符串值。
TradesSelected.Text += (TradesSelected.Text == "" ? "" : ",") + (trade as ListBoxItem).Content.ToString();