首页 > 其他分享 >WPF ItemsControl IsItemsHost true

WPF ItemsControl IsItemsHost true

时间:2024-08-30 15:03:54浏览次数:3  
标签:Windows System elpTbk IsItemsHost new using WPF true public

//customzie control
//xaml
<UserControl x:Class="WpfApp306.ElpTbk"
             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:WpfApp306"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding ElpBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock HorizontalAlignment="Stretch" 
                   VerticalAlignment="Center" 
                   Text="{Binding ElpStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</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 WpfApp306
{
    /// <summary>
    /// Interaction logic for ElpTbk.xaml
    /// </summary>
    public partial class ElpTbk : UserControl
    {
        public ElpTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public SolidColorBrush ElpBrush
        {
            get { return (SolidColorBrush)GetValue(ElpBrushProperty); }
            set { SetValue(ElpBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpBrushProperty =
            DependencyProperty.Register("ElpBrush", typeof(SolidColorBrush),
                typeof(ElpTbk), new PropertyMetadata(new SolidColorBrush(Colors.Brown)));



        public string ElpStr
        {
            get { return (string)GetValue(ElpStrProperty); }
            set { SetValue(ElpStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpStrProperty =
            DependencyProperty.Register("ElpStr", typeof(string),
                typeof(ElpTbk), new PropertyMetadata(""));

    }
}

 

 

 

ItemsHost

//xaml
<Window x:Class="WpfApp306.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:WpfApp306"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">

    <Grid MouseDown="Grid_MouseDown">
        <ListBox BorderBrush="Blue" BorderThickness="3" x:Name="lbx">
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Height" Value="500"/>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Canvas IsItemsHost="True" 
                                    Width="{Binding Path=ActualWidth,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 
                                    Height="500"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
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 WpfApp306
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int idx = 0;
        private Random rnd { get; set; }

        public MainWindow()
        {
            rnd= new Random();
            InitializeComponent();
        }
        
        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ElpTbk elpTbk = new ElpTbk();
            Color cl = new Color();
            cl.A = 255;
            cl.R = (byte)rnd.Next(0, 255);
            cl.G = (byte)rnd.Next(0,255);
            cl.B = (byte)rnd.Next(0, 255);
            elpTbk.ElpBrush = new SolidColorBrush(cl);
            elpTbk.Width = 200;
            elpTbk.Height = 100;
            elpTbk.ElpStr = $"{++idx}___({e.GetPosition(this).X},{e.GetPosition(this).Y})";
            Canvas.SetLeft(elpTbk, e.GetPosition(this).X);
            Canvas.SetTop(elpTbk, e.GetPosition(this).Y);
            lbx.Items.Add(elpTbk);
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:Windows,System,elpTbk,IsItemsHost,new,using,WPF,true,public
From: https://www.cnblogs.com/Fred1987/p/18388787

相关文章

  • [WPF]数据绑定时为何会出现StringFormat失效
    在数据绑定过程中,我们经常会使用StringFormat对要显示的数据进行格式化,以便获得更为直观的展示效果,但在某些情况下格式化操作并未生效,例如Button的Content属性以及ToolTip属性绑定数据进行StringFormat时是无效的。首先回顾一下StringFormat的基本用法。StringFormat的用法Str......
  • [WPF]数据绑定时为何会出现StringFormat失效
    在数据绑定过程中,我们经常会使用StringFormat对要显示的数据进行格式化,以便获得更为直观的展示效果,但在某些情况下格式化操作并未生效,例如Button的Content属性以及ToolTip属性绑定数据进行StringFormat时是无效的。首先回顾一下StringFormat的基本用法。StringFormat的用法Str......
  • 「对比评测」标准WPF DataGrid与DevExpress WPF GridControl有何不同?(二)
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......
  • C# WPF 如何使用折线图方案
    使用WPF的时候经常会出现需要使用折线图、柱状图的情况,一下为折线图的使用方案一、导入NuGet包项目搜索导入LiveCharts.Wpf包二、后端配置折线图需要调用 LineSeries、柱状图调用LineSeries、具体使用图形可参考官方网站 官方网址: LiveCharts这里举例说明折线图的......
  • wpf 触摸 触摸后无法打开pupup
    动态绑定触摸事件btnChangeErase.AddHandler(TouchDownEvent,newRoutedEventHandler(btnChangeErase_TouchDown),true);btnChangeErase.AddHandler(TouchUpEvent,newRoutedEventHandler(btnChangeErase_TouchUp),true);privatevoidbtnChange......
  • WPF-Prism Region使用
    Region:区域,我的理解,就是窗体上的一部分地方,不是整个窗体。所以区域里都是用户控件UserControl,就是为了解决在窗体上显示一个自定义的公用的控件的。共两种用法:1、原始的方法(不借助prism的依赖注入),用IRegionManager接口,来完成区域的注册和设置。注......
  • wpf异常捕获
    protectedoverridevoidOnStartup(StartupEventArgse){Trace.Listeners.Add(newTextWriterTraceListener("ErrorLog.txt"));Trace.AutoFlush=true;AppDomain.CurrentDomain.UnhandledException+=CurrentDomain_UnhandledException;......
  • WPF Livecharts C# Column name change
    //xaml<Windowx:Class="WpfApp299.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • wpf prism用法总结
    1、App文件中RegisterTypes方法:此方法中主要是注册、绑定视图、实体的。使用RegisterForNavigation方法,可以将视图注册成导航,且绑定对应的viewmodel,视图被调用后自动绑定这个viewmodel。注册成导航后,相当于公开了此视图允许调用。同时同一个视图可以绑定多个viewmodel。......
  • 不可不知的WPF画笔(Brush)
    在WPF中,屏幕上的所有内容,都是通过画笔(Brush)画上去的。如按钮的背景色,边框,文本框的前景和形状填充。借助画笔,可以绘制页面上的所有UI对象。不同画笔具有不同类型的输出( 如:某些画笔使用纯色绘制区域,其他画笔使用渐变、图案、图像或绘图)。Brush位于System.Windows.Media命名空间,Br......