我有一个'Rule'类列表,它是DataGrid的源代码。在这个例子中,我有一个列是一个DataGridTemplateColumn,它绑定到'Verified'依赖属性。
我遇到的问题是我有一个VerifyColorConverter,我希望传入所选行的ENTIRE'Rule'对象,这样我就可以检查Rule实例并返回一个合适的画笔。我在设置Border的背景时执行此操作(请参阅下面的代码 - Background =“{Binding Converter = {StaticResource convVerify}}”)
<DataGridTemplateColumn Header="Verified" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource convVerify}}"
CornerRadius="4" Height="17" Margin="2,0,2,0" VerticalAlignment="Center" >
<Grid>
<TextBlock Foreground="Yellow" Text="{Binding Path=Verified, Mode=OneWay}" TextAlignment="Center" VerticalAlignment="Center"
FontSize="11" FontWeight="Bold" />
</Grid>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
当我在DataGrid上设置源代码时,这一切都很有效但是当底层的“Rule”对象被更改时,转换器不会被调用,因此刷子保持不变。当我更改“规则”实例的某些属性时,如何强制更新?
任何帮助表示赞赏!
转换器看起来大致如下:
[ValueConversion(typeof(CRule), typeof(SolidColorBrush))]
public class VerifyColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
CRule rule = value as CRule;
Color clr = Colors.Red;
int count = 0;
int verified = 0;
if (rule != null)
{
count = rule.TotalCount;
verified = rule.NoOfVerified;
}
if (count == 0) clr = Colors.Transparent;
else if (verified == 0) clr = (Color)ColorConverter.ConvertFromString("#FFD12626");
else if (verified < count) clr = (Color)ColorConverter.ConvertFromString("#FF905132");
else clr = (Color)ColorConverter.ConvertFromString("#FF568D3F");
SolidColorBrush brush = new SolidColorBrush(clr);
return brush;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
修改
这是Rule class的一部分:
/// <summary>
/// Compliance Restriction (Rule)
/// </summary>
public class Rule : BindElement
{
public CMode Mode { get; private set; }
public int RuleID { get; private set; }
public string RuleDescription { get; private set; }
private int _NoOfVerified = 0;
private int _TotalCount = 0;
public int NoOfVerified
{
get { return _NoOfVerified; }
set { _NoOfVerified = value; RaiseChanged("Progress"); RaiseChanged("Verified"); }
}
public int TotalCount
{
get { return _TotalCount; }
set { _TotalCount = value; RaiseChanged("Progress"); RaiseChanged("Verified"); }
}
public string Verified
{
get
{
if (TotalCount == 0) return "Nothing to verify";
return string.Format("Verified {0} out of {1}", NoOfVerified, TotalCount);
}
}
答案 0 :(得分:0)
您可以尝试使用MultiValueConverter
代替常规Converter
,并将其传递给您需要的任何规则属性,或者当规则中的某个属性获得时,您可以引发CollectionChanged
事件改变。我通常不喜欢这样做,因为我不知道这会如何影响性能,但这是一个选择。
使用MultiConverter
public object Convert(object[] values, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
Color clr = Colors.Red;
int count = 0;
int verified = 0;
if (values.Count >= 2
&& int.TryParse(count, values[0].ToString())
&& int.TryParse(verfieid, values[1].ToString()))
{
if (count == 0) clr = Colors.Transparent;
else if (verified == 0) clr = (Color)ColorConverter.ConvertFromString("#FFD12626");
else if (verified < count) clr = (Color)ColorConverter.ConvertFromString("#FF905132");
else clr = (Color)ColorConverter.ConvertFromString("#FF568D3F");
}
SolidColorBrush brush = new SolidColorBrush(clr);
return brush;
}
MultiConverter的XAML:
<Border.Background>
<MultiBinding Converter="{StaticResource convVerify}">
<Binding Value="{Binding TotalCount}" />
<Binding Value="{Binding NoOfVerified}" />
</MultiBinding>
</Border.Background>
使用属性更改事件
RulesCollection.CollectionChanged += RulesCollection_Changed;
void RulesCollection_Changed(object sender, CollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach(Rule rule in e.NewItems) // May need a cast here
rule.PropertyChanged += Rule_PropertyChanged;
if (e.OldItems != null)
foreach(Rule rule in e.OldItems) // May need a cast here
rule.PropertyChanged -= Rule_PropertyChanged;
}
void Rule_PropertyChanged()
{
RaisePropertyChanged("RulesCollection");
}
答案 1 :(得分:0)
好的 - 我找到了解决这个问题的方法 - 我所做的是将对象公开为属性,然后在任何更改时为此属性调用OnPropertyChanged。这是由Bind对象正确拾取的,并在属性发生变化时移交给转换器!!
[Serializable()]
public class BindElement : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
public void RaiseChanged(string s)
{
OnPropertyChanged(new PropertyChangedEventArgs(s));
}
#endregion
}
public class BindElement2 : BindElement
{
public void RaiseChanged(string s)
{
base.RaiseChanged(s);
NudgeMyself();
}
public void NudgeMyself()
{
base.RaiseChanged("Myself");
}
}
/// <summary>
/// Compliance Restriction (Rule)
/// </summary>
public class CRule : BindElement2
{
public CMode Mode { get; private set; }
public int RuleID { get; private set; }
public string RuleDescription { get; private set; }
private int _NoOfVerified = 0;
private int _TotalCount = 0;
public int NoOfVerified
{
get { return _NoOfVerified; }
set { _NoOfVerified = value; RaiseChanged("Progress"); RaiseChanged("Verified"); }
}
public int TotalCount
{
get { return _TotalCount; }
set { _TotalCount = value; RaiseChanged("Progress"); RaiseChanged("Verified"); }
}
public string Verified
{
get
{
if (TotalCount == 0) return "Nothing to verify";
return string.Format("Verified {0} out of {1}", NoOfVerified, TotalCount);
}
}
public CRule Myself
{
get { return this; }
}
和其他类可以从BindElement2派生并执行相同的操作:(创建一个暴露实例本身的属性Myself)
public class CTradeRule : BindElement2
{
public CRule Rule { get; set; }
public bool IsLocked { get; set; } // if true this should prevent a Reason from being given
public bool IsVerified { get { return Reason.Length > 0; } }
private string _Reason = ""; // ** no reason **
public string Reason
{
get { return _Reason; }
set { _Reason = value; RaiseChanged("Reason"); }
}
public int Progress
{
get { return (IsVerified ? 1 : 0); }
}
public override string ToString()
{
return string.Format("Rule: {0}, Reason: {1}", Rule.RuleID, _Reason);
}
public CTradeRule Myself
{
get { return this; }
}
}
现在在xaml中我可以这样做:(注意绑定路径=我自己)然后确保整个对象发送到转换器WHENEVER任何属性更改!!
<DataGridTemplateColumn Header="Verified" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="{Binding Path=Myself, Converter={StaticResource convVerify}}"
CornerRadius="4" Height="17" Margin="2,0,2,0" VerticalAlignment="Center" >
<Grid>
<TextBlock Foreground="Yellow" Text="{Binding Path=Verified, Mode=OneWay}" TextAlignment="Center" VerticalAlignment="Center"
FontSize="11" FontWeight="Bold" />
</Grid>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>