【发布时间】:2011-12-15 14:59:47
【问题描述】:
我正在使用客户端对象模型开发 Silverlight Web 部件。我的项目中有一个转换器如下
public class ForeGroundConverter : IValueConverter
{
public ForeGroundConverter()
{
}
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
//return "";
SolidColorBrush result = new SolidColorBrush(Colors.Black);
return result;
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我正在使用此转换器对以下元素进行绑定
<sdk:DataGridTemplateColumn SortMemberPath="ClientName" Header="Client Name" IsReadOnly="True" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ClientName}" Foreground="{Binding Foreground, Converter={StaticResource ForegroundConverter}}"></TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
我在 TimeLog 类中定义了一个属性,如下所示
public SolidColorBrush Foreground {get;set;}
绑定对我来说工作正常。现在我有一个 datagrid 的 loadingrow 事件,如下所示。
SummaryDataGrid_LoadingRow
private void SummaryDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (PaidList.Contains(timeLogObj))
{
int index = PaidList.IndexOf(timeLogObj);
PaidList[index].IsEnabled = false;
PaidList[index].CheckBoxVisibility = Visibility.Collapsed;
PaidList[index].Foreground = new SolidColorBrush(Color.FromArgb(50, 150, 150, 150));
}
}
请看上面代码中的以下行
PaidList[index].Foreground = new SolidColorBrush(Color.FromArgb(50, 150, 150, 150));
在上面的行中,我想为特定行索引动态绑定 textblok 的 Foreground 属性。在这种情况下,我希望转换器将值作为(为特定行索引返回以下值)
new SolidColorBrush(Color.FromArgb(50, 150, 150, 150));
我不知道该怎么做。您能为我提供上述问题的任何代码或链接吗?如果我做错了什么,请指导我。
【问题讨论】:
-
我建议坚持使用您的业务对象而不是使用行索引。您能否让您的对象属性之一负责保留该索引,然后绑定到它?
标签: c# silverlight converter dynamic-binding