【发布时间】: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 给了我这个错误:
【问题讨论】:
-
这是 windows 还是 wpf 应用程序...?
-
这是 WPF,标记为 @MethodMan
-
@RufusL 我已经尝试过该解决方案,但它对我不起作用。
-
你能显示更多代码吗..?特别是您正在使用的 xaml 这可能会有所帮助,但无法用您提供的小代码来判断 stackoverflow.com/questions/17540343/…