我正在开发某种时间跟踪应用程序。
该模型如下所示:
我的DataGrid绑定到
ObservableCollection<Entry>
:
public class Entry : INotifyPropertyChanged
{
public string TicketId {get; set;} //Simplified since working..
private ObservableCollection<TimeBooking> _timebookings;
public ObservableCollection<TimeBooking> TimeBookings
{
get { return _timebookings; }
set
{
_timebookings = value;
OnPropertyChanged();
}
}
public TimeBooking Monday //Same for other days...
{
get
{
return Bookings.Where(b => b.Date == SelectedWeek.FirstDay);
}
}
}
TimeBooking看起来像这样:
public class TimeBooking : INotifyPropertyChanged
{
public TimeBooking(DateTime date)
{
Date = date.Date;
}
public DateTime Date { get; set; }
private TimeSpan _value;
public TimeSpan Value
{
get
{
return _value;
}
set
{
_value = value;
OnPropertyChanged();
}
}
private bool _changed;
public bool Changed //CHANGED SETTER IS INVOKED BY CELL CONTEXT MENU
{
get { return _changed; }
set
{
_changed = value;
OnPropertyChanged();
}
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion Implementation of INotifyPropertyChanged
}
问题在这里:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource EditedConverter}, UpdateSourceTrigger=Explicit}" Value="True">
<Setter Property="Foreground" Value="Pink" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource EditedConverter}, UpdateSourceTrigger=Explicit}" Value="False">
<Setter Property="Foreground" Value="Lime" />
</DataTrigger>
</Style.Triggers>
<Setter Property="ContextMenu" Value="{StaticResource CellContextMenu}" />
</Style>
</DataGrid.CellStyle>
这是我的转换器:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DataGridCell dgc = value as DataGridCell;
if(dgc != null)
{
ENTRY entry = dgc.DataContext as _ENTRY;
if(entry != null)
{
DataGridTextColumn column = dgc.Column as DataGridTextColumn;
if(column != null)
{
var binding = column.Binding as Binding;
if(binding != null && binding.Path != null && binding.Path.Path != null)
{
string val = binding.Path.Path.ToLower();
if (val.StartsWith("monday"))
{
return entry.monday.Changed;
}
if (val.StartsWith("tuesday"))
{
return entry.tuesday.Changed;
}
if (val.StartsWith("wednesday"))
{
return entry.wednesday.Changed;
}
if (val.StartsWith("thursday"))
{
return entry.thursday.Changed;
}
if (val.StartsWith("friday"))
{
return entry.friday.Changed;
}
if (val.StartsWith("saturday"))
{
return entry.saturday.Changed;
}
if (val.StartsWith("sunday"))
{
return entry.sunday.Changed;
}
}
}
}
}
return false;
}
正如我们所看到Binding
到Changed
正在运行(Foreground是Lime),但不会注意到该属性的某些变化..
答案 0 :(得分:1)
如果您希望在filters: [myFilter]
属性设置为新值时调用转换器,则可以使用aa Changed
和多转换器(实现MultiBinding
):
IMultiValueConverter
<Style TargetType="DataGridCell">
<Setter Property="Foreground" Value="Pink" />
<Style.Triggers>
<DataTrigger Value="false">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource multiConverter}">
<Binding Path="Changed" />
<Binding Path="." RelativeSource="{RelativeSource Self}" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="Lime" />
</DataTrigger>
</Style.Triggers>
<Setter Property="ContextMenu" Value="{StaticResource CellContextMenu}" />
</Style>
}
答案 1 :(得分:0)
在看到转换器之后,我可以告诉你,你没有在数据更改时触发转换器的原因是因为你绑定到Control,当你从其中一个改变一个值时,它不会改变你属性。
为了能够实现你想要的,你必须在你的datacontext viewmodel中做一个累积属性,并将触发器绑定到那个或其他方式。
答案 2 :(得分:-2)
试试这个
public ObservableCollection<TimeBooking> TimeBookings
{
get { return _timebookings; }
set
{
_timebookings = value;
OnPropertyChanged("TimeBookings");
}
}
等等。