选择的Listboxitems返回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:part以便它只显示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
对象转换为它实际的类型。 .ToString()
类型的object
方法只返回类型名称。见here。
此外,可能是交易对象的类型,有一个属性/字段/方法,它返回您实际想要显示的字符串值。
TradesSelected.Text += (TradesSelected.Text == "" ? "" : ",") + (trade as ListBoxItem).Content.ToString();