首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#试图在运行时在进度栏中生成两种颜色

c#试图在运行时在进度栏中生成两种颜色
EN

Stack Overflow用户
提问于 2014-04-10 08:59:52
回答 2查看 545关注 0票数 0

我正在尝试使用silverlight-5开发c# web应用程序。

我必须创建一个进度条,它将有两种颜色--绿色(达到一定百分比),然后是红色,这将通过c#中的一些数据操作来动态完成。

我想使它的两种颜色,一定百分比的绿色和休息红色(百分比将在运行时决定)。我读了几份文件,但对我来说很难理解。

有人能帮我用c#写代码吗?(或者,如果此进度条不按运行时获取的值更改颜色,那么请编写一个简单的代码来创建一个自定义进度条,根据运行时的变化值在运行时更改颜色)。会是一个很大的help.Thanks

可能我猜代码将用c#编写,因为在xaml中颜色显示率是固定的,但是它在这2种颜色中的百分比必须根据运行时在c#代码中获得的数据值而改变

注意:我正在使用silverlight和c#开发and应用程序。

所以代码必须像这样写在身体里:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace B
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }


    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-10 16:01:39

您可以将Foreground属性设置为使用在代码隐藏中动态调整的LinearGradientBrush

代码语言:javascript
复制
<ProgressBar x:Name="ThresholdIndicator"/>

代码隐藏:

代码语言:javascript
复制
public MainPage()
{
    InitializeComponent();
    m_thresholdBrush = new LinearGradientBrush(){
                           MappingMode = BrushMappingMode.Absolute};
    GradientStop colorLeftOfThreshold =  new GradientStop(){
                                         Color = Colors.Green,
                                         Offset = 1};
    GradientStop colorRightOfThreshold = new GradientStop(){
                                         Color = Colors.Red,
                                         Offset = 1};
    m_thresholdBrush.GradientStops.Add(colorLeftOfThreshold);
    m_thresholdBrush.GradientStops.Add(colorRightOfThreshold);
    ThresholdIndicator.Foreground = m_thresholdBrush;
    ThresholdIndicator.SizeChanged += (s,e)=>UpdateThresholdBrush();

    //this.ThresholdRatio is your dynamic green/red threshold
    this.ThresholdRatioChanged += (s,e)=>UpdateThresholdBrush();

    UpdateThresholdBrush();
}
private LinearGradientBrush m_thresholdBrush;
private void UpdateThresholdBrush()
{
    m_thresholdBrush.EndPoint = new Point(GetX(), 0);
}
private double GetX()
{
    return this.ThresholdRatio * ThresholdIndicator.ActualWidth;
}

注意,我只更改了EndPoint,两个GradientStops总是位于端点位置。

编辑Ok,我可以看到您与this.ThresholdRatio和更改事件的斗争,这应该可以:

代码语言:javascript
复制
public double ThresholdRatio
{
    get { return (double) GetValue( ThresholdRatioProperty ); }
    set { SetValue( ThresholdRatioProperty, value ); }
}

public static readonly DependencyProperty ThresholdRatioProperty =
    DependencyProperty.Register( "ThresholdRatio", typeof(double),
    typeof( MainPage), new PropertyMetadata(HandleThresholdRatioChanged) );

private static void HandleThresholdRatioChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((MainPage)d).RaiseThresholdRatioChanged();
}

private void RaiseThresholdRatioChanged()
{
    var handlers = ThresholdRatioChanged;
    if (handlers!=null) handlers(this,EventArgs.Empty);
}

public event EventHandler ThresholdRatioChanged;
票数 1
EN

Stack Overflow用户

发布于 2014-04-10 09:12:24

如果您不必使用进度条,那么我认为您最好使用自定义表示形式,例如,使用项目控件,然后为此定义一个数据板,如下所示:

XAML

代码语言:javascript
复制
 <ItemsControl x:Name="ic"
                      BorderBrush="Black" 
                      BorderThickness="2" 
                      Background="Gray" 
                      ItemsSource="{Binding}" 
                      ItemTemplate="{StaticResource test1}" 
                      Height="100" 
                      Width="400">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

数据模板

代码语言:javascript
复制
<DataTemplate x:Key="test1">
            <TextBlock Background="{Binding Color}" 
                       Text="{Binding}" 
                       VerticalAlignment="Center"
                       Padding="2">
                <TextBlock.Width>
                    <MultiBinding Converter="{StaticResource myMultiMarketConverter}">
                        <Binding Path="MarketShare"/>
                        <Binding ElementName="ic" Path="Width" />
                    </MultiBinding>
                </TextBlock.Width>
                <TextBlock.Height>
                    <Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}" />
                </TextBlock.Height>
            </TextBlock>
        </DataTemplate>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22983347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档