WPF - ikonki w drzewku

0

Czy jest konieczna edycja pliku xaml? Chcę ładować drzewko dynamicznie.

1

Wszystko co w xaml da się napisać w c#.

0

Udało się wstawić ikonkę z pliku, ale wszystkie elementy mają taką samą ikonkę. A ja bym chciał w kodzie .cs wstawiać indeks. Czy do tego można wykorzystać item.tag?
Jak przekonwertować w xaml nazwy taga 0,1,2 na nazwy plików?

<TreeView Name="TreeView" Grid.Column="0">
                    <TreeView.Resources>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="HeaderTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Name="img" Width="16" Height="16" Stretch="Fill" Source="green.png"/>
                                            <TextBlock Text="{Binding}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </TreeView.Resources>
                </TreeView>
0

napisz to po polsku. Nikt tego nie zrozumie

Jezeli chcesz operacowac na nazwach plikow a nie na indeksach (na indeksach czego w ogole;o?) to uzyj sobie Dictionary<string, int>

a maja wszystkie taka sama ikonke bo wstales w xaml ikonke. Przenies to

Source="green.png" 

to codu i przypisuj grafike jaka komu chcesz

PS
Ja cos czuje ze probujesz zrobic cos co nie ma sensu. Moze opisalbys co chcesz zrobic (ogolnie), a nie "jak zrobic to TYM sposobem?" Bo czuje, ze Twoj sposob nie jest dobry do problemu

0
 <RibbonWindow ...
        xmlns:local="clr-namespace:mySQLProjekt"
...
        >    
...
                        <TreeView Name="listaBaz" MinWidth="100" FontSize="15">
                         <TreeView.Resources>
                          <Style TargetType="{x:Type TreeViewItem}">
                           <Setter Property="HeaderTemplate">
                           <Setter.Value>
                          <DataTemplate>
                         <StackPanel Orientation="Horizontal">
                         <Image Name="img"  Width="15" Height="15" Stretch="Fill" 
                         Source="{Binding 
                         RelativeSource={RelativeSource 
                         Mode=FindAncestor, 
                         AncestorType={x:Type TreeViewItem}}, 
                         Path=Tag, 
                Converter={x:Static local:TagToImageConverter.Instance}}"       
               />
              <TextBlock Text="{Binding}" Margin="5,0" />
             </StackPanel>
          </DataTemplate>
         </Setter.Value>
       </Setter>
      </Style>
   </TreeView.Resources>
 </TreeView>
...
namespace mySQLProjekt
{
    [ValueConversion(typeof(string), typeof(bool))]
    class TagToImageConverter : IValueConverter
    {
        public static TagToImageConverter Instance = new TagToImageConverter();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((value as string).Contains("BAZA"))
            {
                BitmapImage source = new BitmapImage(new Uri("pack://application:,,,/Images/download_database.png"));
                return source;
            }
            else if ((value as string).Contains("TABELA"))
            {
                BitmapImage source = new BitmapImage(new Uri("pack://application:,,,/Images/table_1.png"));
                return source;
            }
            else if ((value as string).Contains("COLUMNA"))
            {
                BitmapImage source = new BitmapImage(new Uri("pack://application:,,,/Images/application_view_columns.png"));
                return source;
            }
            else return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
} 
0

Dzięki, ta odpowiedź jest bliska temu co zrobiłem bazując na http://www.thebestcsharpprogrammerintheworld.com/blogs/Adding-images-to-a-Tree-View-in-WPF.aspx

xmlns:local="clr-namespace:MojeKategorieWPF"

    <Window.Resources>
        <local:TagImageConwerter x:Key="TagImageConwerter" />
    </Window.Resources> 
<TreeView Name="TreeView" Grid.Column="0">
                    <TreeView.Resources>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="HeaderTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Name="img" Width="16" Height="16" Stretch="Fill" 
                                                   Source="{Binding RelativeSource=
                                                {RelativeSource Mode=FindAncestor, 
                                                AncestorType={x:Type TreeViewItem}},Path=Tag, Converter={StaticResource TagImageConwerter}}"/>
                                            <TextBlock Text="{Binding}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </TreeView.Resources>
                </TreeView> 
 class TagImageConwerter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string imagePath;
            if ((int)value == 0)
                imagePath = "green.png";
            else
                imagePath = "loop.png";
            BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/" + imagePath));
            return image;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1