[英]Silverlight - Changing button text color created dynamically
我正在嘗試使用ViewModel更改Silverlight表單上動態創建的按鈕的按鈕文本顏色。 我面臨的問題是,當我更改按鈕文本的顏色時,所有按鈕都會生效。 由於按鈕是動態創建的,因此無法對其進行控制。
建議我在模型中編寫一個ForegroundColor屬性,然后將其附加到按鈕上,就像您在代碼中看到的那樣,但是對此卻無能為力。
您不確定我正在做什么,並且不確定(我不確定),我會以正確的方式幫助您提出建議。
謝謝
模型
namespace Web.Models
{
[DataContract(IsReference = true)]
public class Sales
{
[DataMember]
public int SalesId { get; set; }
[DataMember]
public int ShowOrder { get; set; }
[DataMember]
public bool Active { get; set; }
[DataMember]
public bool Regurgitate { get; set; }
[DataMember]
public int ForegroundColor { get; set; }
public Sales(Salese result)
{
SalesId = result.SalesId;
ShowOrder = result.ShowOrder;
Active = result.Active;
Regurgitate = result.Regurgitate;
if (SalesId == 12)
{
var bytes = System.BitConverter.GetBytes(ForegroundColor);
Color btnColor = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
SolidColorBrush myBrush = new SolidColorBrush(btnColor);
}
}
}
}
視圖模型
private Brush _foregroundColor = new SolidColorBrush(Colors.Black);
public override void Loaded()
{
OnMainOutcome();
}
public Brush ForegroundColor
{
get { return _foregroundColor; }
set
{
if (_foregroundColor == value) return;
_foregroundColor = value;
OnPropertyChanged("ForegroundColor");
}
}
private void OnMainOutcome()
{
var selectedSalesId = (int)OutcomeCommand.CommandParameter;
CurrentSubOutcomes = GetCurrentSubOutcomes(selectedSalesId);
foreach (var index in CurrentOutcomes)
{
if (index.OutcomeId == 12)
ForegroundColor = new SolidColorBrush(Colors.Red);
else
ForegroundColor = new SolidColorBrush(Colors.Black);
}
}
XAML編輯
<controls:ChildWindow.Resources>
<converters:NumericToColorConverter x:Key="NumericToColorConverter"/>
</controls:ChildWindow.Resources>
<ListBox Grid.Row="1" Height="Auto" MinHeight="200" Width="160" Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=CurrentOutcomes}" Background="{x:Null}" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Height="30" Width="150" HorizontalAlignment="Center" Content="{Binding Outcome}" CommandParameter="{Binding SalesOutcomeId }" Command="{Binding Source={StaticResource ViewModel}, Path=OutcomeCommand}" Foreground="{Binding Source={StaticResource ViewModel}, Converter={StaticResource NumericToColorConverter}, Path=ForegroundColor}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
轉換器類NEW
using System;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
namespace Converters
{
public class NumericToColorConverter : IValueConverter
{
static readonly SolidColorBrush RED_BRUSH = new SolidColorBrush(Colors.Red);
static readonly SolidColorBrush BLUE_BRUSH = new SolidColorBrush(Colors.Blue);
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
//Int32 id = System.Convert.ToInt32(value);
//LinearGradientBrush brush = new LinearGradientBrush();
//brush.StartPoint = new Point(0, 1);
//brush.EndPoint = new Point(0, 0);
//brush.GradientStops.Add(new GradientStop()
//{
// Color = Colors.White,
// Offset = 0
//});
//brush.GradientStops.Add(new GradientStop()
//{
// Color = Color.FromArgb(
// 200,
// System.Convert.ToByte((id * 103) % 256),
// System.Convert.ToByte((id * 157) % 256),
// System.Convert.ToByte((id * 233) % 256)
// ),
// Offset = 1
//});
//return brush;
var OutcomeId = (int)value;
if (OutcomeId == 12)
{
return RED_BRUSH;
}
else
{
return BLUE_BRUSH;
}
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
您的列表框顯示Sales
的列表,但是每個項目都經過模板化以將按鈕前景綁定到VM中的單個屬性。 在您的Sales
類中創建MyBrush
屬性並綁定到它。
namespace Web.Models
{
[DataContract(IsReference = true)]
public class Sales
{
[DataMember]
public int SalesId { get; set; }
[DataMember]
public int ShowOrder { get; set; }
[DataMember]
public bool Active { get; set; }
[DataMember]
public bool Regurgitate { get; set; }
[DataMember]
public int ForegroundColor { get; set; }
[DataMember]
public SolidColorBrush MyBrush { get; set; }
public Sales(Salese result)
{
SalesId = result.SalesId;
ShowOrder = result.ShowOrder;
Active = result.Active;
Regurgitate = result.Regurgitate;
if (SalesId == 12)
{
var bytes = System.BitConverter.GetBytes(ForegroundColor);
Color btnColor = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
MyBrush = new SolidColorBrush(btnColor);
}
}
}
<ListBox Grid.Row="1" Height="Auto" MinHeight="200" Width="160" Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=CurrentOutcomes}" Background="{x:Null}" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Height="30" Width="150" HorizontalAlignment="Center" Content="{Binding Outcome}" CommandParameter="{Binding SalesOutcomeId }" Command="{Binding Source={StaticResource ViewModel}, Path=OutcomeCommand}" Foreground="{Binding MyBrush}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您將Foreground
綁定到StaticResource
上的值,以便所有按鈕都將綁定到該相同值。
您可以創建特定的“ ButtonForegroundConverter”,也可以將Brush屬性添加到項目級別的視圖模型中,該模型還具有已經綁定到按鈕的Content
的Outcome
屬性。 然后按鈕xaml將如下所示:
<Button ...
Content="{Binding Outcome}"
Foreground="{Binding OutcomeForegroundBrush}"
...
/>
如果直接綁定到實體,則添加這樣的屬性不是一個好主意,因此上面的示例假定您具有一個中間視圖模型或控制器,可以在其上添加這樣的屬性。
如果您選擇使用轉換器,它將看起來像這樣:
<Button ...
Content="{Binding Outcome}"
Foreground="{Binding OutcomeId, Converter={StaticResource OutcomeToForegroundConverter}}"
...
/>
和轉換器:
namespace MyConverters {
public sealed class OutcomeToColorConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
// We must have a valid integer value, double check bindings
if (value == null) {
throw new ArgumentNullException("value",
"Please make sure the value is not null.");
}
var OutcomeId = (int)value;
if (OutcomeId == XXX) {
return RED_BRUSH;
}
else {
return BLUE_BRUSH;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
static readonly SolidColorBrush RED_BRUSH = new SolidColorBrush(Colors.Red);
static readonly SolidColorBrush BLUE_BRUSH = new SolidColorBrush(Colors.Blue);
}
}
記住聲明資源:
<myconverters:OutcomeToForegroundConverter x:Key="OutcomeToForegroundConverter" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.