I am binding some business objects to a WPF ItemsControl. They are displayed using a custom IValueConverter implementation used to produce the Geometry for a Path object in the DataTemplate as shown here:
<ItemsControl x:Name="Display"
Background="White"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding ElementName=ViewPlaneSelector,
Path=SelectedItem.VisibleElements}"
>
<ItemsControl.Resources>
<!-- This object is just used to get around the fact that ConverterParameter
can't be a binding directly (it's not a DependencyProperty on a DependencyObject -->
<this:GeometryConverterData
x:Key="ConverterParameter2"
Plane="{Binding ElementName=ViewPlaneSelector,
Path=SelectedItem}" />
<DataTemplate DataType="{x:Type o:SlenderMember}">
<Path Stroke="Blue" StrokeThickness=".5"
Data='{Binding Converter={StaticResource SlenderMemberConverter},
ConverterParameter={StaticResource ConverterParameter2}}'
ToolTip="{Binding AsString}">
</Path>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
Note that the items for the ItemsControl are drawn from the ViewPlaneSelector (a ComboBox) SelectedItem.VisibleElements property. I need that same ViewPlaneSelector.SelectedItem in the SlenderMemberConverter to figure out how to display this element. I'm trying to get a reference to it into the converter by creating the intermediate GeometryConverterData object in the Resources section. This object exists solely to get around the problem of not being able to bind directly to the ConverterParameter property (as mentioned in the comments). Here is the code for the GeometryDataConverter class:
class GeometryConverterData : FrameworkElement {
public static readonly DependencyProperty PlaneProperty =
DependencyProperty.Register("Plane", typeof(ViewPlane),
typeof(GeometryConverterData), null, ValidValue);
public static bool ValidValue(object o){
return true;
}
public ViewPlane Plane {
get{
return GetValue(PlaneProperty) as ViewPlane;
}set{
SetValue(PlaneProperty, value);
}
}
}
I added the ValidValue function for debugging, to see what this property was getting bound it. It only and always gets set to null. I know that the ViewPlaneSelector.SelectedItem isn't always null since the ItemsControl has items, and it's items are drawn from the same property on the same object... so what gives? How can I get a reference to this ComboBox into my ValueConverter.
Or, alternately, why is what I'm doing silly and overly complicated. I'm as guilty as many of sometimes getting it into my head that something has to be done a certain way and then killing myself to make it happen when there's a much cleaner and simpler solution.