【问题标题】:Change Label Text to ListBox Selected Item's Text将标签文本更改为列表框选定项的文本
【发布时间】:2015-06-13 22:45:18
【问题描述】:

问题

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

我尝试过使用:

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

但按钮会显示“Windows.System...”

如果列表框的模板与它有任何关系,它就会被编辑。这么简单的问题我想不通。

列表框的 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 给了我这个错误:

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

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

我觉得

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

会做的。

【讨论】:

    【解决方案2】:
    Window1.FoodLabel.Text = (ListBoxItem)this.ListBox1.SelectedValue).Content.ToString();
    

    【讨论】:

      【解决方案3】:

      你可以绑定它:

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

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

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

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-03
        相关资源
        最近更新 更多