WPF ItemsPanelTemplate per Click WrapPanel.Orientation chang

Ilja Strecke 21 Reputation points
2022-03-18T09:51:48.74+00:00

Hallo
Ich möchte per Click-Event die Orientation meines WrapPanel ändern und habe keine Ahnung wie ich an das Panel ran komme.
Mein Xaml-Code,

        <ListBox x:Name="LB" Height="160" Width="90" DockPanel.Dock="Left" SelectionChanged="LB_SelectionChanged"   >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Vertical" x:Name="WP" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Height="50" Width="50" Source="{Binding Path=UriSource}" Stretch="Fill" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DockPanel>
    <Button x:Name="BTN_Rotation" Content="Dreh" Click="BTN_Rotation_Click"  Grid.Row="1" />

Vielen Dank schon im Voraus.
Ilja

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-03-18T11:42:15.817+00:00

    If you want to switch the orientation of the WrapPanel between vertical and horizontal with the click of a button, you could try the following code. If I misunderstood, please let me know.

    MainWindow.xaml:

      <Grid>  
            <Grid.RowDefinitions>  
                <RowDefinition/>  
                <RowDefinition/>  
            </Grid.RowDefinitions>  
            <DockPanel>  
                <ListBox x:Name="LB" Height="160" Width="90" DockPanel.Dock="Left" SelectionChanged="LB_SelectionChanged"   >  
                <ListBox.ItemsPanel>  
                    <ItemsPanelTemplate>  
                        <WrapPanel Background="AliceBlue"  Orientation="Vertical" x:Name="WP" />  
                    </ItemsPanelTemplate>  
                </ListBox.ItemsPanel>  
                <ListBox.ItemTemplate>  
                    <DataTemplate>  
                        <Image Height="50" Width="50" Source="{Binding Path=UriSource}" Stretch="Fill" />  
                    </DataTemplate>  
                </ListBox.ItemTemplate>  
            </ListBox>  
            </DockPanel>  
            <Button x:Name="BTN_Rotation" Content="Dreh" Click="BTN_Rotation_Click"  Grid.Row="1" />  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System;  
    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Media;  
    
    namespace ListBoxOrientationChangedPerClick  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
          list.Add(new MyImage() { UriSource=@"6.jpg" });  
          list.Add(new MyImage() { UriSource=@"6.jpg" });  
    
          LB.ItemsSource = list;  
        }  
        ObservableCollection<MyImage> list=new ObservableCollection<MyImage>();  
        private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject  
        {  
          for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)  
          {  
            var child = VisualTreeHelper.GetChild(obj, i);  
            if (child != null && child is T)  
              return (T)child;  
            var childOfChild = FindVisualChild<T>(child);  
            if (childOfChild != null)  
              return childOfChild;  
          }  
          return null;  
        }  
        private void BTN_Rotation_Click(object sender, RoutedEventArgs e)  
        {  
          WrapPanel wrapPanel = FindVisualChild<WrapPanel>(LB);  
          if(wrapPanel.Orientation== Orientation.Horizontal)  
          {  
            wrapPanel.Orientation=Orientation.Vertical;  
          }  
          else  
          {  
            wrapPanel.Orientation = Orientation.Horizontal;  
          }  
        }  
        private void LB_SelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
        }  
      }  
      public class MyImage : INotifyPropertyChanged  
      {  
        private string _img;  
        public string UriSource  
        {  
          get { return _img; }  
          set  
          {  
            _img = value;  
            NotifyPropertyChanged();  
          }  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
        {  
          if (PropertyChanged != null)  
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
        }  
      }  
    }  
    

    The result:
    184545-image.png
    184537-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ilja Strecke 21 Reputation points
    2022-03-18T13:25:41.28+00:00

    Vielen Dank für deine Antwort,
    leider Bekomme ich einen Fehler. Vielleicht liegt es an meiner Art die Bilder an das Binding zu binden:

    public static class ImageData
    {

        public static List<BitmapImage> LoadImage()  
        {  
            List<BitmapImage> _bilder = new List<BitmapImage>();  
            DirectoryInfo _BildOrdner = new DirectoryInfo(@"C:\Testbilder");  
            foreach (FileInfo Testbilder in _BildOrdner.GetFiles("*.png"))  
            {  
                Uri uri = new Uri(Testbilder.FullName);  
                _bilder.Add(new BitmapImage(uri));  
            }  
    
            return _bilder;  
        }  
    
    }  
    

    184602-image.png

    0 comments No comments

  2. Ilja Strecke 21 Reputation points
    2022-03-18T13:47:34.323+00:00

    Sorry Fehler gefunden. i statt einer 1. Wer lesen kann ist klar im Vorteil.

    0 comments No comments

  3. Ilja Strecke 21 Reputation points
    2022-03-18T14:03:49.537+00:00

    Eine Frage hätte ich noch, lässt sich das Gleiche mit dem DockPanel anwenden um die ListBox neu anzudocken?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.