I'm trying to change the Text of a Label on another window to the value of a selected item within a listbox on the first window.
I've tried using:
Window1.FoodLabel.Text= this.ListBox1.SelectedItem.ToString();
But the button will say "Windows.System..."
The ListBox's Template is edited if that has anything to do with it. I can't figure anything out to such simple problem.
<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>
I've tried this as well, which seemed like it would work:
Window1 window1 = new Window1();
window1.FoodLabel.Content = (ListBoxItem)this.ListBox1.SelectedValue.Content.ToString();
window1.Show();
But Visual Studio gives me this error:
Am I understanding the question correctly -- you just want to be able to get the text for the selected ListBoxItem?
I would think
Window1.FoodLabel.Text= (string) this.ListBox1.SelectedItem.Content;
would do it.
\n\nWindow1.FoodLabel.Text = (ListBoxItem)this.ListBox1.SelectedValue).Content.ToString();\n
You can bind it :
Text="{Binding ElementName=ListBox1, Path=SelectedItem.Content}"
Or you can set Text on SelectionChanged event:
TbTimeDispPopup.Text = (string)((ListBoxItem)ListBox1.SelectedItem).Content;
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.