我是 WPF 应用程序的新手,我尝试ListBox
在单击某个项目时更改我的图像源TreeView
,但我ListBox
在此事件上的始终为空,所以我不能做那件事!
这是我的代码,请告诉我我的错误!提前致谢 .... !
<ScrollViewer Background="#FF3FA7E8">
<TreeView Foreground="#FF28D81C" Height="254" Width="97" SelectedItemChanged="Menu_change" Name="menu">
<TreeViewItem Header="Movie" IsExpanded="True">
<TreeViewItem Header="Fast anh Furious" IsSelected="True" Name="fast"></TreeViewItem>
<TreeViewItem Header="Con giáp"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Sport">
<TreeViewItem Header="MU" Name="MU"></TreeViewItem>
<TreeViewItem Header="Chữ cái"></TreeViewItem>
<TreeViewItem Header="Tình yêu"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="facebook">
<TreeViewItem Header="troll"></TreeViewItem>
<TreeViewItem Header="Chữ cái"></TreeViewItem>
<TreeViewItem Header="Tình yêu"></TreeViewItem>
</TreeViewItem>
</TreeView>
</ScrollViewer>
<ListBox Name="lbTodoList" Width="185" Height="254" HorizontalContentAlignment="Stretch" SelectionMode="Extended" SelectionChanged="lbTodoList_SelectionChanged">
<ListBox.Resources>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="4" CornerRadius="5" Margin="6" >
<Image Source="{Binding}" Stretch="Fill" Width="50" Height="60" />
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
</ListBox.Resources>
</ListBox>
这是 Menu_change 事件
public void Menu_change(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeViewItem TrV = (TreeViewItem)menu.SelectedValue;
string selected = ((TreeViewItem)menu.SelectedValue).Name.ToString();
TreeViewItem TrVParrent = (TreeViewItem)TrV.Parent;
string parrent = TrVParrent.Header.ToString();
string path = "image\\img_" + parrent.ToLower() + "\\" + parrent.ToLower() + "_" + selected;
lbTodoList.ItemsSource = LoadImages(path); // this lbTodoList always null
}