将标签文本更改为ListBox所选项目的文本

时间:2015-04-08 19:32:09

标签: c# .net wpf

问题

我正在尝试将另一个窗口上的标签文本更改为第一个窗口中列表框中所选项目的值。

我尝试过使用:

Window1.FoodLabel.Text= this.ListBox1.SelectedItem.ToString();

但按钮会说“Windows.System ...”

如果ListBox的模板与它有关,则对其进行编辑。我无法解决这个简单的问题。

ListBox的XAML:

<ListBox x:Name="ListBox1" Margin="0,23,0,43" Background="#FF313131" Style="{DynamicResource Seeds}" BorderThickness="0,1,1,1" SelectionChanged="ListBox1_SelectionChanged">
            <ListBoxItem x:Name="Barley" Content="Barley" HorizontalAlignment="Center" Style="{DynamicResource SeedItem}" Selected="Barley_Selected"/>
            <ListBoxItem Content="Bean" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle1}"/>
            <ListBoxItem Content="BlueBerry" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle2}"/>
            <ListBoxItem Content="Chili" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle3}"/>
            <ListBoxItem Content="Corn" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle4}"/>
            <ListBoxItem Content="Cucumber" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle5}"/>
            <ListBoxItem Content="Garlic" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle6}"/>
            <ListBoxItem Content="Millet" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle7}"/>
            <ListBoxItem Content="Oat" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle8}"/>
            <ListBoxItem Content="Onion" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle9}"/>
            <ListBoxItem Content="Peanut" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle10}"/>
            <ListBoxItem Content="Potato" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle11}"/>
            <ListBoxItem Content="Pumpkin" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle12}"/>
            <ListBoxItem Content="Quinoa" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle13}"/>
            <ListBoxItem Content="Rice" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle14}"/>
            <ListBoxItem Content="Rye" HorizontalAlignment="Center" Style="{DynamicResource SeedItem}"/>
            <ListBoxItem Content="Strawberry" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle15}"/>
            <ListBoxItem Content="Tomato" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle16}"/>
            <ListBoxItem Content="Wheat" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle17}"/>
            <ListBoxItem Content="Yam" HorizontalAlignment="Center" Style="{DynamicResource ListBoxItemStyle18}"/>
        </ListBox>

我也试过这个,看起来它会起作用:

 Window1 window1 = new Window1();
          window1.FoodLabel.Content = (ListBoxItem)this.ListBox1.SelectedValue.Content.ToString();
          window1.Show();

但是Visual Studio给了我这个错误:

enter image description here

3 个答案:

答案 0 :(得分:0)

我是否正确理解了这个问题 - 您只是希望能够获取所选ListBoxItem的文本?

我会想

Window1.FoodLabel.Text= (string) this.ListBox1.SelectedItem.Content;

会这样做。

答案 1 :(得分:0)

Window1.FoodLabel.Text = (ListBoxItem)this.ListBox1.SelectedValue).Content.ToString();

答案 2 :(得分:0)

你可以绑定它:

Text="{Binding ElementName=ListBox1, Path=SelectedItem.Content}"

或者您可以在SelectionChanged事件上设置Text:

TbTimeDispPopup.Text = (string)((ListBoxItem)ListBox1.SelectedItem).Content;

希望这有帮助!