首页 > 其他分享 >WPF dependency property to customize control in usercontrol

WPF dependency property to customize control in usercontrol

时间:2024-06-11 22:59:51浏览次数:14  
标签:control dependency customize List System ImageListBox Windows using public

//usercontrol
<UserControl x:Class="WpfApp157.ImageListBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp157"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" x:Name="lbx">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Width="200" Height="500" Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Image Grid.Column="1" x:Name="img" Source="{Binding SelectedItem,ElementName=lbx}">            
        </Image>
    </Grid>
</UserControl>


//usercontrol.cs
using System;
using System.Collections.Generic;
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;

namespace WpfApp157
{
    /// <summary>
    /// Interaction logic for ImageListBox.xaml
    /// </summary>
    public partial class ImageListBox : UserControl
    {
        public List<string> LbxItemSource
        {
            get { return (List<string>)GetValue(LbxItemSourceProperty); }
            set { SetValue(LbxItemSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LbxItemSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LbxItemSourceProperty =
            DependencyProperty.Register("LbxItemSource", typeof(List<string>), 
                typeof(ImageListBox), new PropertyMetadata(null,LbxItemsSourceChangedCallBack));

        private static void LbxItemsSourceChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var lbxControl = d as ImageListBox;
            if(lbxControl == null)
            {
                return;
            }
            lbxControl.lbx.ItemsSource = e.NewValue as List<string>;
            lbxControl.lbx.SelectedIndex = 0;
        }

        public ImageListBox()
        {
            InitializeComponent();
        }
    }
}

 

 

//main.xaml
<Window x:Class="WpfApp157.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:WpfApp157"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:ImageListBox LbxItemSource="{Binding  ImgsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>


//main.xaml.cs
using System;
using System.Collections.Generic;
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;

namespace WpfApp157
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

         public List<string> ImgsList {  get; set; }
        public MainWindow()
        {
            InitializeComponent();
            var files = Directory.GetFiles(@"..\..\Images")?.ToList();
            ImgsList = new List<string>(files);
            this.DataContext = this;
        }
    }
}

 

 

 

标签:control,dependency,customize,List,System,ImageListBox,Windows,using,public
From: https://www.cnblogs.com/Fred1987/p/18242977

相关文章

  • NET8中增加的简单适用的DI扩展库Microsoft.Extensions.DependencyInjection.AutoActiv
    这个库提供了在启动期间实例化已注册的单例,而不是在首次使用它时实例化。单例通常在首次使用时创建,这可能会导致响应传入请求的延迟高于平时。在注册时创建实例有助于防止第一次Request请求的SLA以往我们要在注册的时候启动单例可能会这样写://注册:services.AddSingleton<Fil......
  • c# NuGet中安装了Vlc.DotNet.Forms库 工具箱中没有vlcControl控件???
    在C#的WindowsForms应用程序中,使用NuGet包管理器安装了Vlc.DotNet.Forms库后,如果在工具箱(Toolbox)中没有发现VlcControl控件,这通常意味着控件没有被正确注册或者没有被识别。解决方法:确认Vlc.DotNet.Forms库已正确安装。可以通过检查项目的packages文件夹和project.json文件来......
  • ControlNet++:让AI图像生成更精准、更可控
     在人工智能的世界里,文本到图像的生成技术正变得越来越先进。但如何确保生成的图像精确地反映我们的想象呢?最近,一项名为ControlNet++的新技术为我们提供了答案。ControlNet++是一种新颖的方法,它通过优化生成图像与给定条件之间的像素级循环一致性,显著提高了文本到图像生成的......
  • k8s学习--ingress详细解释与应用(nginx ingress controller))
    文章目录lngress简介什么是IngressIngress的用途Ingress的工作原理Ingress的工作流程Ingress的应用场景应用实验环境部署nginxingresscontroller1.安装metalLB2.nginxingresscontroller部署3.ingress对象应用案例(基于名称的负载均衡)(1)创建deployment控制......
  • WebviewController进行混合开发,鸿蒙星河版API(11)
    @ohos.web.webview提供web控制能力,web组件提供网页显示的能力,同时也可以执行网页中定义的JS方法。一、第一步创建WebviewController实例controller:WebviewController=newwebview.WebviewController()二、web组件加载html文件build(){Navigation(){Col......
  • Spring Boot 3太强:全新Controller接口定义方式
    环境:SpringBoot3.2.51.回顾定义接口方式1.1常规定义@RestController@RequestMapping("/users")publicclassUsersController{  @PostMapping()  public Object save(@RequestBodyUsers users) {//...}}99.9999%的人都是这样定义接口。1.2好......
  • DevExpress WPF中文教程:Grid - 如何向项目添加GridControl并绑定到数据
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......
  • ComfyUI 完全入门:ControlNet 使用教程
    大家好,我是每天分享AI应用的萤火君!今天继续给大家分享ComfyUI的入门必备技能:ControlNet。ControlNet提供了十几种生成图片的控制方式,有的可以控制画面的结构,有的可以控制人物的姿势,还有的可以控制图片的画风,这对于提高AI绘画的质量特别有用;基于ControlNet的能力,炼丹师们可......
  • Error creating bean with name 'userController': Unsatisfied dependency expressed
    SSM整合项目搭建时,项目启动报错,报错内容如下:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'userController':Unsatisfieddependencyexpressedthroughfield'userService';nestedexceptionisorg.springfra......
  • Microsoft.Extensions.DependencyInjection会自动释放通过DI(依赖注入)创建的对象
    Microsoft.Extensions.DependencyInjection中(下面简称DI),在调用ServiceProvider和IServiceScope对象的Dispose()方法时,也会自动调用ServiceProvider和IServiceScope对象通过DI创建的对象的Dispose()方法(前提是,通过DI创建的对象实现了IDisposable接口)。我们新建一个.NETCore控制台......