Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight-更改动态创建的按钮文本颜色_C#_Silverlight_Xaml - Fatal编程技术网

C# Silverlight-更改动态创建的按钮文本颜色

C# Silverlight-更改动态创建的按钮文本颜色,c#,silverlight,xaml,C#,Silverlight,Xaml,我正在尝试使用ViewModel更改Silverlight窗体上动态创建的按钮的按钮文本颜色。我面临的问题是,当我更改按钮文本的颜色时,所有按钮都会生效。因为按钮是动态创建的,所以我无法对其进行控制 有人建议我在模型中写一个ForegroundColor属性,然后附加到按钮上,正如您在代码中看到的那样,我尝试了,但做不了多少 你能看看我在做什么,帮我提建议吗?因为我不确定,我做得对 谢谢 型号 namespace Web.Models { [DataContract(IsReference

我正在尝试使用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);
    }
 }
}

视图模型

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>

}

您的列表框显示了一个销售列表,但每个项目都是模板化的,用于将按钮前景绑定到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);

        }
   }
}


您将
前台
绑定到
静态资源
上的一个值,因此所有按钮都将绑定到此相同的值

您可以创建特定的“ButtonForgroundConverter”,也可以将笔刷属性添加到项目级viewmodel,该viewmodel还具有已绑定到按钮的
内容的
结果
属性。然后按钮xaml将如下所示:

<Button ...
        Content="{Binding Outcome}" 
        Foreground="{Binding OutcomeForegroundBrush}" 
        ...
        />
<Button ...
        Content="{Binding Outcome}" 
        Foreground="{Binding OutcomeId, Converter={StaticResource OutcomeToForegroundConverter}}" 
        ...
        />
请记住声明资源:

<myconverters:OutcomeToForegroundConverter x:Key="OutcomeToForegroundConverter" />


非常感谢您的帮助。实际上,在发布了这个问题之后,我尝试以相同的方式实现,但是在转换器类中,它给出了关于
(int)值的错误,因为特定强制转换无效。为什么呢?我正在更新上面的源代码,使用与您类似的代码。请看一看并提出建议。嗯,
int
不能从
null
强制转换。I您必须修复绑定,以便始终获得非空值,或者首先将转换器更改为检查
null
。请检查该值是否正确绑定。提示:查看输出窗口中的文本,这些文本会用更具体的信息指示任何绑定错误。我可以将模型对象(类)作为参数绑定到转换器并访问其值吗?我很难将值传递给转换器并根据转换器进行评估,因为它无法识别我传递的参数。我尝试绑定几乎所有的值,虽然我现在没有得到任何错误,但我将无法在converter类中进行计算。有什么建议吗?另外,我可以调试converter类,以便查看实际传递的内容吗?当然可以调试
转换器。单击要停止的位置,在函数中放置断点,然后按F9。然后再次运行以查看代码停止在那里。也可以将对象作为参数传递,但是我认为如果您在转换器内的断点处开始调试,并查看输出,您可能会发现绑定中存在可以修复的问题。当您停在那里时,也可以查看“局部变量”窗口中的值。(以及日志的输出窗口)感谢所有的输入,我现在已经解决了这个问题。我所做的只是从按钮绑定中删除了
Source={StaticResource ViewModel}
,之后工作得很好,但我仍然不明白,这是怎么回事?谢谢你的建议,我尝试了你的代码片段,但什么都没发生,它不调用MyBrush属性,并给出错误“远程服务器返回错误:NotFound”。为什么是tht?但是当我注释行“//[DataMember]//public SolidColorBrush MyBrush{get;set;}”时,错误发生了,什么也没有发生,这意味着按钮文本保持不变。可能
SolidColorBrush
在序列化时遇到了问题。您可以尝试将颜色值存储为简单类型,并将
IValueConverter
与绑定一起使用。
<myconverters:OutcomeToForegroundConverter x:Key="OutcomeToForegroundConverter" />