Hi Guys
I asked this question the other day.
I got only one answer which was to make my own control and add a dependency property to vary the only thing that changes (an index to get the right data bindings in this case).
If that's the only way to do it though, it raises some other questions. Specifically, if you find yourself wanting to write a custom control as a convenience for a limited use, how do you handle: 1) Resources 2) DataContext
I like the idea of keeping the xaml as unbloated as possible. but my WPF bag of tricks is pretty limited at this point. I wrote code like you see below, which looks awkward.
Any suggestions?
Cheers,
Berryl
public class DayColumn : DataGridTextColumn
{
#region DayIndex DP
// omitted
#endregion
public DayColumn(ActivityViewModel model) {
_setCellStyle();
_setBindingForCells(model);
... // more of the same
}
private void _setCellStyle() {
CellStyle = (Style) Application.Current.Resources["DataEntryCellStyle"];
}
private void _setBindingForCells(ActivityViewModel model) {
var cellsBinding = new Binding();
var amtConv = new AmountConverter();
cellsBinding.Converter = amtConv;
// would this even work??
cellsBinding.Path = new PropertyPath(
string.Format("Allocations[{1}]", DayIndex));
}
}
The xaml I'm envisioning could be as simple as this:
<local:DayColumn DayIndex="1" />
== SPECIFICALLY ==
In the code you can see I am looking up the resource. If this was 'normal' (outside of the WPF framework, icluding XAML) code I would likely try to pass what is needed in during construction. Can I count on Application.Current.Resources to have what I need? Can I test it? If that isn't reliable and testable, how would I get the resources in there?