我正在尝试使用silverlight-5开发c# web应用程序。
我必须创建一个进度条,它将有两种颜色--绿色(达到一定百分比),然后是红色,这将通过c#中的一些数据操作来动态完成。
我想使它的两种颜色,一定百分比的绿色和休息红色(百分比将在运行时决定)。我读了几份文件,但对我来说很难理解。
有人能帮我用c#写代码吗?(或者,如果此进度条不按运行时获取的值更改颜色,那么请编写一个简单的代码来创建一个自定义进度条,根据运行时的变化值在运行时更改颜色)。会是一个很大的help.Thanks
可能我猜代码将用c#编写,因为在xaml中颜色显示率是固定的,但是它在这2种颜色中的百分比必须根据运行时在c#代码中获得的数据值而改变
注意:我正在使用silverlight和c#开发and应用程序。
所以代码必须像这样写在身体里:
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();
}
}
}发布于 2014-04-10 16:01:39
您可以将Foreground属性设置为使用在代码隐藏中动态调整的LinearGradientBrush。
<ProgressBar x:Name="ThresholdIndicator"/>代码隐藏:
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和更改事件的斗争,这应该可以:
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;发布于 2014-04-10 09:12:24
如果您不必使用进度条,那么我认为您最好使用自定义表示形式,例如,使用项目控件,然后为此定义一个数据板,如下所示:
XAML
<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>数据模板
<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>https://stackoverflow.com/questions/22983347
复制相似问题