无法从树视图,WPF上的所选项目更改事件访问控件

时间:2013-08-03 05:03:46

标签: wpf treeview

我是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            
}

0 个答案:

没有答案