I have a List of 'Rule' classes that is the source of a DataGrid. In this example I have one of the columns which is a DataGridTemplateColumn that is bound to the 'Verified' dependency property.
我有一个'Rule'类列表,它是DataGrid的源代码。在这个例子中,我有一个列是DataGridTemplateColumn,它绑定到'Verified'依赖项属性。
The problem I am having is that I have a VerifyColorConverter where I wish to pass in the ENTIRE 'Rule' object of the selected row so I can examine the Rule instance and return an appropriate brush. I do this in when setting the background of the Border (see code below - Background="{Binding Converter={StaticResource convVerify}}")
我遇到的问题是我有一个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>
This all works well when I set the source on the DataGrid but when the underlying 'Rule' object is altered the converter is not called upon so the brush stays the same. How can I force this to be updated when I alter some of the properties of the 'Rule' instance?
当我在DataGrid上设置源代码时,这一切都很有效,但是当底层的“Rule”对象被更改时,转换器不会被调用,因此刷子保持不变。当我更改“规则”实例的某些属性时,如何强制更新此更新?
Any help appreciated!
任何帮助赞赏!
Converter looks roughly like this:
转换器看起来大致如下:
[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
}
EDIT
This is part of Rule class:
这是Rule类的一部分:
/// <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);
}
}
2 个解决方案
#1
0
You could try using a MultiValueConverter
instead of a regular Converter
, and pass it whatever Rule Properties you need, or you can raise a CollectionChanged
event when a property on the Rule gets changed. I usually prefer not to do this since I don't know how this affects performance, but it's an option.
您可以尝试使用MultiValueConverter而不是常规Converter,并将其传递给您需要的任何规则属性,或者您可以在规则上的属性发生更改时引发CollectionChanged事件。我通常不喜欢这样做,因为我不知道这会如何影响性能,但它是一种选择。
Using a MultiConverter
使用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;
}
XAML for MultiConverter:
XAML for MultiConverter:
<Border.Background>
<MultiBinding Converter="{StaticResource convVerify}">
<Binding Value="{Binding TotalCount}" />
<Binding Value="{Binding NoOfVerified}" />
</MultiBinding>
</Border.Background>
Using Property Change Events
使用属性更改事件
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");
}
#2
0
OK - I found a way around this - what I have done is expose the object as a property and then call the OnPropertyChanged for this property whenever anything changes. This is picked up properly by the Bind object and handed over to the Converter whenever a property changes!!
好的 - 我找到了解决这个问题的方法 - 我所做的是将对象公开为属性,然后在发生任何变化时为此属性调用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; }
}
and other classes can derived from BindElement2 and do the same: (create a property Myself that exposes the instance itself)
和其他类可以从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; }
}
}
Now in the xaml I can do this: (note the Binding Path=Myself) which then ensures the entire object is send to the converter WHENEVER any property changes!!
现在在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>
#1
0
You could try using a MultiValueConverter
instead of a regular Converter
, and pass it whatever Rule Properties you need, or you can raise a CollectionChanged
event when a property on the Rule gets changed. I usually prefer not to do this since I don't know how this affects performance, but it's an option.
您可以尝试使用MultiValueConverter而不是常规Converter,并将其传递给您需要的任何规则属性,或者您可以在规则上的属性发生更改时引发CollectionChanged事件。我通常不喜欢这样做,因为我不知道这会如何影响性能,但它是一种选择。
Using a MultiConverter
使用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;
}
XAML for MultiConverter:
XAML for MultiConverter:
<Border.Background>
<MultiBinding Converter="{StaticResource convVerify}">
<Binding Value="{Binding TotalCount}" />
<Binding Value="{Binding NoOfVerified}" />
</MultiBinding>
</Border.Background>
Using Property Change Events
使用属性更改事件
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");
}
#2
0
OK - I found a way around this - what I have done is expose the object as a property and then call the OnPropertyChanged for this property whenever anything changes. This is picked up properly by the Bind object and handed over to the Converter whenever a property changes!!
好的 - 我找到了解决这个问题的方法 - 我所做的是将对象公开为属性,然后在发生任何变化时为此属性调用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; }
}
and other classes can derived from BindElement2 and do the same: (create a property Myself that exposes the instance itself)
和其他类可以从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; }
}
}
Now in the xaml I can do this: (note the Binding Path=Myself) which then ensures the entire object is send to the converter WHENEVER any property changes!!
现在在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>