WPF Element Width Height is percent of Parent element via converter ,converterparameter

//converter
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WpfApp380
{
    public class SizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToDouble(value)*System.Convert.ToDouble(parameter);
        }

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


 <Window.Resources>
     <local:SizeConverter x:Key="sizeConverer"/>
 </Window.Resources>


 <Image Source="{Binding ImgUrl}" 
        Width="{Binding Converter={StaticResource sizeConverer},
     Path=ActualWidth,
     RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},
     ConverterParameter=0.5}"
        Height="{Binding Converter={StaticResource sizeConverer},
     Path=ActualHeight,
        RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},
     ConverterParameter=0.5}"/>

 

 

 

 

 

 

//conveter
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WpfApp380
{
    public class SizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToDouble(value)*System.Convert.ToDouble(parameter);
        }

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


//xaml
<Window x:Class="WpfApp380.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp380" 
        mc:Ignorable="d" 
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:BookVM/>
    </Window.DataContext>
    <Window.Resources>
        <local:SizeConverter x:Key="sizeConverer"/>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 VirtualizingPanel.IsContainerVirtualizable="True"
                 VirtualizingPanel.VirtualizationMode="Recycling"
                 VirtualizingPanel.CacheLength="100"
                 VirtualizingPanel.CacheLengthUnit="Item">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Image Source="{Binding ImgUrl}" 
                               Width="{Binding Converter={StaticResource sizeConverer},
                            Path=ActualWidth,
                            RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},
                            ConverterParameter=0.5}"
                               Height="{Binding Converter={StaticResource sizeConverer},
                            Path=ActualHeight,
                               RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},
                            ConverterParameter=0.5}"/>
                        <TextBlock Text="{Binding Name}" FontSize="50" Foreground="Red"
                                   FontWeight="ExtraBlack"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;

namespace WpfApp380
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class BookVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public BookVM()
        {
            InitData();
        }

        private void InitData()
        {
            var imgsList = Directory.GetFiles(@"../../Images");
            if (imgsList != null && imgsList.Any())
            {
                int imgsCount = imgsList.Count();
                BooksCollection = new ObservableCollection<Book>();
                for(int i=0;i<1000000;i++)
                {
                    BooksCollection.Add(new Book()
                    {
                        Id = i,
                        Name = $"Name_{i}",
                        Title=$"Title_{i}",
                        Topic=$"Topic_{i}",
                        ISBN=$"ISBN_{i}",
                        ImgUrl = imgsList[i%imgsCount]
                    });
                }
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if(value!=booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged(nameof(BooksCollection));
                }
            }
        }
    }


    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Title { get; set; }

        public string Topic { get; set; }

        public string ISBN { get; set; }

        public string ImgUrl { get; set; }
    }
}

 

posted @ 2024-09-19 21:18  FredGrit  阅读(2)  评论(0编辑  收藏  举报