在WPF中选择Excel颜色 [英] Select Excel color in WPF

查看:53
本文介绍了在WPF中选择Excel颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有操纵Excel文件的WPF应用程序.用户可以用不同的方式在Excel中标记内容,但是着色是我将在此处重点介绍的一种.因此,例如,假设用户将单元格A1的颜色设置为红色(默认情况下Excel知道红色的方式为颜色) 选择).现在,在WPF中,用户需要显示红色的单元格列表(为简单起见,假设它正好是一个单元格),即此处为A1. WPF应用程序中允许用户选择映射到Excel颜色的最聪明的方法是什么?我当然可以 说在此文本框中键入颜色的名称",但这似乎太容易出错-更好的方法是让用户从调色板中进行选择.但是,您有更好的方法吗?我知道Excel的工作到此结束,所以这就是为什么我问

I have WPF application that manipulates Excel-files. A user can mark things in Excel with different ways, but coloring is the one I'm going to concentrate here. So for example, let's say user colors cell A1 as red (the way Excel knows red by default color selection). Now in WPF user needs to display a list of cells (for simplicity, let's say it is exactly one cell) that are red i.e. A1 here. What is the smartest way in a WPF application to allow user select a color that maps to the Excels colors? Sure, I can say "type into this text box the name of the color", but that seems too error prone - a lot better way would be to let user pick from a palette of colors. But do you have an even better way? I know the Excel's end of this, so that's why I'm asking this in WPF forum.

推荐答案

以下是从ComboBox中选择颜色的XAML:

The following is the XAML to select a colour from a ComboBox:

<Window x:Class="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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider MethodName="GetType" ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
            <ObjectDataProvider.MethodParameters>
                <sys:String>System.Windows.Media.Colors, PresentationCore,
                Version=3.0.0.0, Culture=neutral,
                PublicKeyToken=31bf3856ad364e35</sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"
        MethodName="GetProperties" x:Key="colorPropertiesOdp">
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <ComboBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" Height="30" VerticalAlignment="Top" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Rectangle Fill="{Binding Path=Name}" Stroke="Black" Margin="0,0,10,0" StrokeThickness="1" Width="30"></Rectangle>
                        <TextBlock  Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>


这篇关于在WPF中选择Excel颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆