WPF 用户控件、Binding、全屏、Tooltip、鼠标样式、水印等

09-用户控件的使用

<UserControl x:Class="UserControlDemo.MyProgress"
             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:UserControlDemo"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Height="330" Width="80">
    <StackPanel Orientation="Vertical">
        <ProgressBar Orientation="Vertical" Maximum="100" Minimum="0" Value="12" Height="300"></ProgressBar>
        <TextBlock Height="30" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">A1</TextBlock>
    </StackPanel>
</UserControl>Code language: HTML, XML (xml)
<Window x:Class="UserControlDemo.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:UserControlDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Horizontal">
        <local:MyProgress Margin="20" />
        <local:MyProgress Margin="20" />
        <local:MyProgress Margin="20" />

    </StackPanel>
</Window>Code language: HTML, XML (xml)

10-用户控件绑定数据源

<UserControl x:Class="UserControlDemo.MyProgress"
             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:UserControlDemo"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Height="330" Width="80">
    <StackPanel Orientation="Vertical">
        <ProgressBar Orientation="Vertical" Maximum="{Binding Max}" Minimum="0" Value="{Binding Value}" Height="300"></ProgressBar>
        <TextBlock Height="30" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">A1</TextBlock>
    </StackPanel>
</UserControl>Code language: HTML, XML (xml)
<Window x:Class="UserControlDemo.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:UserControlDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Horizontal" Name="sp">
        <local:MyProgress Margin="20" DataContext="{Binding ViewModel.Count[0]}" />
        <local:MyProgress Margin="20" DataContext="{Binding ViewModel.Count[1]}" />
        <local:MyProgress Margin="20" DataContext="{Binding ViewModel.Count[2]}" />
    </StackPanel>
</Window>Code language: HTML, XML (xml)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
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 UserControlDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ProgressViewModel viewModel;
        public ProgressViewModel ViewModel
        {
            get
            {
                if (viewModel==null)
                {
                    viewModel = new ProgressViewModel();
                }
                return viewModel;
            }
            set
            {
                viewModel = value;
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            ViewModel.Count.Add(0, new Detail() {Max = 10,Value = 2 });
            ViewModel.Count.Add(1, new Detail() { Max = 20, Value = 6 });
            ViewModel.Count.Add(2, new Detail() { Max = 100, Value = 0});

            this.sp.DataContext = this;

            new Thread(new ThreadStart(Deal)) { IsBackground = true }.Start();
        }

        private void Deal()
        {
            while (true)
            {
                ViewModel.Count[0].Value++;
                ViewModel.Count[1].Value++;
                ViewModel.Count[2].Value++;

                //因为Count是一个字典 是属于引用类型 引用类型如果要改变 则需要new 因为在盏内存 是不会改变到的 
                ViewModel.Count = new Dictionary<int, Detail>(ViewModel.Count);

                Thread.Sleep(200);
            }
            
        }
    }

    public class ProgressViewModel:INotifyPropertyChanged
    {
        private Dictionary<int, Detail> count;
        public Dictionary<int, Detail> Count
        {
            get
            {
                if (count==null)
                {
                    count = new Dictionary<int, Detail>();
                }
                return count;
            }

            set
            {
                count = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Detail : INotifyPropertyChanged
    {
        private int value;

        public int Value
        {
            get
            {
                return value;
            }

            set
            {
                this.value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public int Max
        {
            get
            {
                return max;
            }

            set
            {
                max = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Max"));
            }
        }

        private int max;

        public event PropertyChangedEventHandler PropertyChanged;
    }
}Code language: HTML, XML (xml)

11-动态创建多个用户控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
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 UserControlDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ProgressViewModel viewModel;
        public ProgressViewModel ViewModel
        {
            get
            {
                if (viewModel==null)
                {
                    viewModel = new ProgressViewModel();
                }
                return viewModel;
            }
            set
            {
                viewModel = value;
            }
        }

        private Random random = new Random();

        public MainWindow()
        {
            InitializeComponent();

            //准备数据源
            for (int i = 0; i < 5; i++)
            {
                ViewModel.Details.Add(i, new Detail() { Max = 50, Value = i });
            }

            //创建用户控件对象
            for (int i = 0; i < 5; i++)
            {
                MyProgress mp = new MyProgress();
                
                Binding bind = new Binding()
                {
                    Source = ViewModel,
                    Path = new PropertyPath("Details[" + i + "]")
                };
                BindingOperations.SetBinding(mp, MyProgress.DataContextProperty, bind);

                this.sp.Children.Add(mp);
            }
           
            this.sp.DataContext = this;

            new Thread(new ThreadStart(Deal)) { IsBackground = true }.Start();
        }

        private void Deal()
        {
            while (true)
            {
                int index = random.Next(0, ViewModel.Details.Count);
                ViewModel.Details[index].Value++;

                //因为Count是一个字典 是属于引用类型 引用类型如果要改变 则需要new 因为在盏内存 是不会改变到的 
                ViewModel.Details = new Dictionary<int, Detail>(ViewModel.Details);

                Thread.Sleep(200);
            }
            
        }
    }

    public class ProgressViewModel:INotifyPropertyChanged
    {
        private Dictionary<int, Detail> count;
        public Dictionary<int, Detail> Details
        {
            get
            {
                if (count==null)
                {
                    count = new Dictionary<int, Detail>();
                }
                return count;
            }

            set
            {
                count = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Detail : INotifyPropertyChanged
    {
        private int value;

        public int Value
        {
            get
            {
                return value;
            }

            set
            {
                this.value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public int Max
        {
            get
            {
                return max;
            }

            set
            {
                max = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Max"));
            }
        }

        private int max;

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

12-WPF中Binding使用StringFormat格式化字符串方法

<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Value, StringFormat={}{0:C}}" FontSize="35" Margin="2"></TextBlock>
        <TextBlock Text="{Binding Value, StringFormat={}{0}元}" FontSize="35" Margin="2"></TextBlock>
        <TextBlock Text="{Binding Value, StringFormat={}递四方速递{0}你好吗}" FontSize="35" Margin="2"></TextBlock>
    </StackPanel>
</Window>Code language: HTML, XML (xml)
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;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel myViewModel;


        public ViewModel MyViewModel
        {
            get
            {
                if (myViewModel==null)
                {
                    myViewModel = new ViewModel();
                }
                return myViewModel;
            }

            set
            {
                myViewModel = value;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            MyViewModel.Value = 1234;
            this.DataContext = this.MyViewModel;
        }
    }

    public class ViewModel: INotifyPropertyChanged
    {
        private int value;

        public int Value
        {
            get
            {
                return value;
            }

            set
            {
                this.value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}Code language: HTML, XML (xml)

13-使用多态创建不同的用户控件

使用多态方式 创建不同的用户控件。

UserControl uc;
if (model.IsDouble)
{
    uc = new BigFrame();
}
else
{
    uc = new SmallFrame();
}Code language: JavaScript (javascript)
//创建用户控件对象
            for (int i = 0; i < 5; i++)
            {
                UserControl mp;
                if (i%2==0)
                {
                    mp = new MyProgress();
                }
                else
                {
                    mp = new MyProgress1();
                }
                
                Binding bind = new Binding()
                {
                    Source = ViewModel,
                    Path = new PropertyPath("Details[" + i + "]")
                };
                BindingOperations.SetBinding(mp, MyProgress.DataContextProperty, bind);

                this.sp.Children.Add(mp);
            }Code language: JavaScript (javascript)

14-MultiBinding多重绑定

我们可以一个控件绑定数据源的多个字段

<TextBlock  Grid.Row="0" Grid.Column="0" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,5" Foreground="Black">
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0}/{1}">
                    <Binding Path="Index[0].Count" />
                    <Binding Path="Index[0].Max" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>

        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="其他文字:{0}{1}">
                    <Binding Path="Value" />
                    <Binding Path="Name" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>Code language: HTML, XML (xml)
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;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel myViewModel;


        public ViewModel MyViewModel
        {
            get
            {
                if (myViewModel==null)
                {
                    myViewModel = new ViewModel();
                }
                return myViewModel;
            }

            set
            {
                myViewModel = value;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            MyViewModel.Value = 1234;
            MyViewModel.Name = "北盟网校";
            this.DataContext = this.MyViewModel;
        }
    }

    public class ViewModel: INotifyPropertyChanged
    {
        private int value;

        public int Value
        {
            get
            {
                return value;
            }

            set
            {
                this.value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }

        private string name;

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                this.name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}Code language: HTML, XML (xml)

15-WPF全屏窗口设计

public Window2()
{
    InitializeComponent();

    // 设置全屏
    this.WindowState = System.Windows.WindowState.Normal;
    this.WindowStyle = System.Windows.WindowStyle.None;
    this.ResizeMode = System.Windows.ResizeMode.NoResize;
    this.Topmost = true;

    this.Left = 0.0;
    this.Top = 0.0;
    this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
    this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
}Code language: JavaScript (javascript)

按esc退出

private void Mywin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
            }Code language: JavaScript (javascript)

16-动态添加Tooltip

我们使用用户控件 设置一个用户控件

<UserControl x:Class="WpfApp2.MyToolTip"
             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:WpfApp2"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <Style TargetType="Label">
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition  Width="180"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0" Grid.Row="0">名称:</Label>
        <Label Grid.Column="0" Grid.Row="1">最后入库日期:</Label>
        <Label Grid.Column="0" Grid.Row="2">数量:</Label>
        <Label Grid.Column="0" Grid.Row="3">发出地:</Label>
        <Label Grid.Column="0" Grid.Row="4">其他:</Label>

        <Label Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left">非常多文字文字</Label>
        <Label Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字很多文字</Label>
        <Label Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字</Label>
        <Label Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left">很多文字很多文字很多文字</Label>
        <Label Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字</Label>
    </Grid>
</UserControl>Code language: HTML, XML (xml)

然后后台添加控件的时候 动态设置

uc.ToolTip = new MyToolTip();Code language: JavaScript (javascript)

17-Tooltip设置停留时间

uc.ToolTip = new MyToolTip();
ToolTipService.SetShowDuration(uc, 30000);Code language: JavaScript (javascript)

设置uc控件的ToolTip有效时间为 30秒

18-WPF设置窗体鼠标样式

注意,鼠标样式是一个文件配置的

Windows的鼠标文件格式有两种,一种是静态文件(.cur),另一种是动态文件(.ani)

其中 cur文件 本身我们操作系统就有 大家可以

通常在 C:\Windows\Cursors 这个目录下面

这些文件 我们可以使用Photoshop等工具自己设计 网上也有很多下载的

下面我们来讲解一下 如何在WPF中自定义

public MainWindow()
{
    Cursor cur = new Cursor(@"C:\Users\86186\source\repos\WpfApp2\WpfApp2\aero_pen.cur");
    this.Cursor = cur;
    InitializeComponent();
}Code language: JavaScript (javascript)

19-WPF窗口水印效果

我们希望在窗口中显示一个半透明的水印文字,当然也可以是图片。

<UserControl x:Class="WpfApp2.MyToolTip"
             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:WpfApp2"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <Style TargetType="Label">
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>
    <GroupBox Header="详细信息" Margin="20">
        <Grid>
            <Label FontSize="200" Opacity="0.05" Foreground="Gray">BAMN.CN</Label>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  Width="180"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Grid.Row="0" FontWeight="Bold">名称:</Label>
                <Label Grid.Column="0" Grid.Row="1" FontWeight="Bold">可用库位数量:</Label>
                <Label Grid.Column="0" Grid.Row="2" FontWeight="Bold">数量:</Label>
                <Label Grid.Column="0" Grid.Row="3" FontWeight="Bold">发出地:</Label>
                <Label Grid.Column="0" Grid.Row="4" FontWeight="Bold">其他:</Label>

                <Label Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Content="{Binding Name}"></Label>
                <Label Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left"  Content="{Binding Remaint}"></Label>
                <Label Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字</Label>
                <Label Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left">很多文字很多文字很多文字</Label>
                <Label Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字</Label>
            </Grid>
        </Grid>
    </GroupBox>
</UserControl>Code language: HTML, XML (xml)

如果文字 需要渐变颜色

<UserControl x:Class="WpfApp2.MyToolTip"
             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:WpfApp2"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <Style TargetType="Label">
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>

        <Style x:Key="textBlockStyle" TargetType="TextBlock">
            <!--字体渐变-->
            <Setter Property="Foreground">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                        <GradientStop Color="Red" Offset="0.0" />
                        <GradientStop Color="Blue" Offset="0.5" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="200"/>
            <Setter Property="Opacity" Value="0.05"/>
        </Style>
    </UserControl.Resources>
    <GroupBox Header="详细信息" Margin="20">
        <Grid>
            <TextBlock Style="{StaticResource textBlockStyle}">BAMN.CN</TextBlock>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  Width="180"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Grid.Row="0" FontWeight="Bold">名称:</Label>
                <Label Grid.Column="0" Grid.Row="1" FontWeight="Bold">可用库位数量:</Label>
                <Label Grid.Column="0" Grid.Row="2" FontWeight="Bold">数量:</Label>
                <Label Grid.Column="0" Grid.Row="3" FontWeight="Bold">发出地:</Label>
                <Label Grid.Column="0" Grid.Row="4" FontWeight="Bold">其他:</Label>

                <Label Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Content="{Binding Name}"></Label>
                <Label Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left"  Content="{Binding Remaint}"></Label>
                <Label Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字</Label>
                <Label Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left">很多文字很多文字很多文字</Label>
                <Label Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left">很多文字很多文字很多文字很多文字</Label>
            </Grid>
        </Grid>
    </GroupBox>
</UserControl>Code language: HTML, XML (xml)

20-Tooltip绑定数据源动态显示

我们希望绑定控件的Tooltip控件 也要跟着显示动态对应的内容。下面我们来进度条控件的Tooltip增加对应的数据源绑定。

修改tooltip的xaml

<Label Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Content="{Binding Name}"></Label>Code language: HTML, XML (xml)

然后绑定mytooltip

var ttp = new MyToolTip();
                mp.ToolTip = ttp;
                ToolTipService.SetShowDuration(mp, 30 * 1000);

                Binding bind = new Binding()
                {
                    Source = ViewModel,
                    Path = new PropertyPath("Details[" + i + "]")
                };
                BindingOperations.SetBinding(mp, MyProgress.DataContextProperty, bind);
                BindingOperations.SetBinding(ttp, MyProgress.DataContextProperty, bind);Code language: JavaScript (javascript)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注