首页 > 其他分享 >WPF Text MultiBinding StringFormat

WPF Text MultiBinding StringFormat

时间:2024-05-02 21:24:24浏览次数:30  
标签:Convert ToByte Windows Text System values using WPF MultiBinding


<TextBlock.Text>
<MultiBinding StringFormat="R:{0:N0},G:{1:N0},B:{2:N0}">
<Binding Path="Value" ElementName="_red"/>
<Binding Path="Value" ElementName="_green"/>
<Binding Path="Value" ElementName="_blue"/>
</MultiBinding>
</TextBlock.Text>

 
//Whole code
<Window x:Class="WpfApp86.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:WpfApp86"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:ColorMultiConverter x:Key="colorConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
            <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
        <Slider  Minimum="0" Maximum="255" Margin="4" x:Name="_red" Grid.Row="0"                   
 TickFrequency="1" IsSnapToTickEnabled="True" />
        <Slider Minimum="0" Maximum="255" Margin="4" x:Name="_green" Grid.Row="1"  
TickFrequency="1" IsSnapToTickEnabled="True" />
        <Slider Minimum="0" Maximum="255" Margin="4" x:Name="_blue" Grid.Row="2" 
TickFrequency="1" IsSnapToTickEnabled="True" />
        <Rectangle Grid.Row="3">
            <Rectangle.Fill>
                <MultiBinding Converter="{StaticResource colorConverter}">
                    <Binding Path="Value" ElementName="_red"/>
                    <Binding Path="Value" ElementName="_green"/> 
                    <Binding Path="Value" ElementName="_blue"/>
                </MultiBinding>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock Grid.Row="4" FontSize="30" VerticalAlignment="Center"
            HorizontalAlignment="Center">
            <TextBlock.Text>
                <MultiBinding StringFormat="R:{0:N0},G:{1:N0},B:{2:N0}">
                    <Binding Path="Value" ElementName="_red"/>
                    <Binding Path="Value" ElementName="_green"/>
                    <Binding Path="Value" ElementName="_blue"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Globalization;
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 WpfApp86
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class ColorMultiConverter : IMultiValueConverter
    {
        //brush.Color = Color.FromRgb(System.Convert.ToByte(values[0]),
        //        System.Convert.ToByte(values[1]),
        //        System.Convert.ToByte(values[2]));
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Color.FromRgb(System.Convert.ToByte(values[0]),
                System.Convert.ToByte(values[1]), System.Convert.ToByte(values[2]));
            return brush;
        }

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

 

 

 

 

<TextBlock.Text>
    <MultiBinding StringFormat="R:{0:N0},G:{1:N0},B:{2:N0}">
        <Binding Path="Value" ElementName="_red"/>
        <Binding Path="Value" ElementName="_green"/>
        <Binding Path="Value" ElementName="_blue"/>
    </MultiBinding>
</TextBlock.Text>

 

标签:Convert,ToByte,Windows,Text,System,values,using,WPF,MultiBinding
From: https://www.cnblogs.com/Fred1987/p/18170587

相关文章

  • WPF MultiBinding
    <Windowx:Class="WpfApp84.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF Slider Show integral value TickFrequency="1" IsSnapToTickEnabled="True"
    <Windowx:Class="WpfApp85.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource GroupDescriptions GroupStyle ItemsPanelTemplate
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource GroupDescriptions PropertyGroupDescription GroupStyle
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF CollectionViewSource ICollectionViewLiveShaping IsLiveSorting
    <Windowx:Class="WpfApp82.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF DataContext="{Binding SelectedItem,ElementName=_master}"
    <Windowx:Class="WpfApp80.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF DataTemplate DataTrigger
    <Windowx:Class="WpfApp79.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • Servlet中的Config和Context
    ServletConfig在servlet对象创建之后创建,每有一个servlet对象就有对应的servletConfig对象。ServletContext在Tomcat服务器加载Web项目后由Tomcat创建,一个web项目在Tomcat的启动运行中只有一个Context对象。ServletContext对象:ServletContext是一个全局对象,代表整个Web应......
  • TextMeshPro - 基本使用
    1 选中字体文件(这边使用了华文行楷),然后创建FontAsset创建后会生成一个TMP用的字体资源 2,创建一个TextMeshPro的Text将字体资源设置为刚刚创建的那个,就可以开始使用了 3,粗体,斜体,下划线,删除线,文字颜色设置  4,字符间距,单词间距,行间距,段落间距的控制 5, 文字......
  • WPF多语言支持:简单灵活的动态切换,让你的程序支持多国语言
     概述:本示例演示了在WPF应用程序中实现多语言支持的详细步骤。通过资源字典和数据绑定,以及使用语言管理器类,应用程序能够在运行时动态切换语言。这种方法使得多语言支持更加灵活,便于维护,同时提供清晰的代码结构。在WPF中实现多语言的一种常见方法是使用资源字典和数据绑定。......