Binding from resources inside a DataTemplate - wpf

Is there some way to get the DataContext of a DataTemplate to use in bindings within its resources?
<DataTemplate x:Key="History">
<ItemsControl ItemsSource="{Binding History}">
<ItemsControl.Resources>
<app:BitmapProvider x:Key="Converter" ShowDetails="True"
Type="{Binding Model.Type}" />
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Data, Converter={StaticResource Converter}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
The above template is used as the CellTemplate of a ListBox. The object at that level has two properties, History (containing a list of "historic info" objects) and Model (containing a bunch of other stuff, including Type). I'm using an ItemsControl to display the historic items next to each other; I want to display an image for each one, and the image is obtained from the BitmapProvider, which is an IValueConverter.
The converter needs two bits of info to obtain a result: one is the Data of the individual historic items, and the other is the Type of the whole collection. An added complication is that constructing this particular converter (or changing the Type given to it) is expensive, so I don't want to put it at the level of the individual history item, or to use a MultiBinding, and I can't put it outside of the template because then it won't have access to the Type.
Unfortunately, the above gives me the following error:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Model.Type; DataItem=null; target element is 'BitmapProvider' (HashCode=57142809); target property is 'Type' (type 'TypeDetails')
Which I understand to mean that the resource can't figure out how to get the DataContext of the element it's contained within.
(I have searched, and most of the answers I could find suggested moving it outside the template or using a MultiBinding instead -- neither of which would really work in this case, as far as I can tell, as I've explained above. But I'd be delighted to be proven wrong, or given another alternative.)

I think you can accomplish that with DataContextSpy.
try something like:
<ItemsControl.Resources>
<spy:DataContextSpy x:Key="Spy"/>
<app:BitmapProvider x:Key="Converter" ShowDetails="True"
Type="{Binding DataContext.Model.Type,Source={StaticResource Spy}}" />
</ItemsControl.Resources>

Related

how to prevent using global DataTemplate without specifiying new one

<DataTemplate DataType="{x:Type MyType}">
...
</DataTemplate>
I have a default DataTemplate for MyType.
want to prevent using it below without having to specify a real DataTemplate
<ItemsControl ItemsSource="{whateverList of MyType}" ItemTemplate="{x:Null}"/>
ItemTemplate="{x:Null}" doesn't get the job done -> shows default DataTemplate
would be happy with "ToString()" display
any ideas?
If you want to override the default data template for the type, you will have to specify a different one:
<ItemsControl ItemsSource="{listOfMyType}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- Whatever -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
There is some more discussion about this approach here, including another's attempt at using {x:Null} for the template.
If your template needs to bind to the ToString() for your type (and no property exists in the type to do this for you), you'll need to use an IValueConverter, as discussed here.

split DataTemplate in different grids using ItemTemplateSelector

I am working on WPF-XAML. My requirement is :
I need to add collection of Trunks(which consists of Border & TexBlocks) in a Tab.
there will be 2 types of such Trunks (say RSPTrunkTemplate and ASPTrunkTemplate). now I need to add collection of Trunks of type RSPTrunkTemplate in one grid. then there will be GridSplitter and then I need to add another collection of Trunks of type ASPTrunkTemplate in another grid.
I am using ItemTemplateSelector as follows :
<Grid>
<ItemsControl Name="TrunkList"
ItemsSource="{Binding RSPTrunks}"
ItemTemplateSelector="{StaticResource TrunkItemTemplateSelector}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
this TrunkItemTemplateSelector is as follows :
<Helpers:TrunkItemTemplateSelector x:Key="TrunkItemTemplateSelector"
RSPTrunkTemplate="{StaticResource RSPTrunkTemplate}"
SPTrunkTemplate="{StaticResource ASPTrunkTemplate}" />
Now, RSPTrunkTemplate should be in one grid and ASPTrunkTemplate shoulb be in another grid.
How to do this. Do I have to change my approach.?
I seek your help guys.
ItemTemplateSelector, as the name suggests, is used to specify a different template for the objects inside a ItemsControl, not to do filtering. If i understand correctly you want to apply a grouping maybe this link can help you http://msdn.microsoft.com/en-us/library/ms742542.aspx

ItemsControl with only custom subelements

I would like to build a custom component that layouts its childs in either a StackPanel or a Grid (with variable row count, which makes me consider the StackPanel instead). The items are custom elements/objects that just hold some configuration, based on which a few controls are created to display them (some labels and text boxes).
Ideally, the component should be used somehow like this (where SpecializedCustomPanelItem is a subtype of CustomPanelItem):
<CustomPanel>
<CustomPanelItem Param1="value A" Param2="value B">Text</CustomPanelItem>
<CustomPanelItem Param1="value C">Other text</CustomPanelItem>
<SpecializedCustomPanelItem>More text</SpecializedCustomPanelItem>
<!-- The number of items is variable -->
</CustomPanel>
I’ve read on the ItemsControl for a while now, and it fits my needs rather well. I would create simply types for the items, and make data templates for them available from inside the ItemsControl. Then they should already render fine.
However I would like to require the items inside that ItemsControl to be of a specific type (i.e. CustomPanelItem or a subtype). I actually thought that the ItemsControl would allow this, just like you within a ComboBox or a MenuItem, but it turns out that it actually allows any subtype, and if necessary wraps them in a item container.
So I have been thinking if an ItemsControl is actually what I am looking for, as I do not want any “fancy” things like selection or scrolling which most of those controls implement. I actually only want to build a simple interface to a common pattern in the application that auto generates those components and layouts them in a Grid/StackPanel.
Should I still be using the ItemsControl or rather build some more custom component?
In this case you don't really need a custom component. Changing the ItemsPanel type to whatever type you need + multiple templates for the Items should do the trick.
However to answer the question in the heading: If you want to force an items control to only accept a certain type of items, you will have to create
a. A CustomItemsControl
b. A CustomItemsControlItem
Then for the CustomItemsControl you should declare the attribute
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(CustomItemsControlItem))]
Then you also will need to
protected override DependencyObject GetContainerForItemOverride()
{
return new CustomItemsControlItem();
// You can throw an exception here
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is CustomItemsControlItem;
}
If memory serves this should force the ItemsControl to not allow other types to be added as children and should throw exceptions. You could then do some magic inside CustomItemsControlItem by defining some DependencyProperties which you can then set when adding the items in XAML.
But yet if you have multiple types in your ViewModel that you want to display correctly, the correct way is still to provide multiple templates for the CustomItemsControlItem targetting your ViewModel types.
Hope this helps.
This sounds perfect for an ItemsControl
You can set it's ItemsPanelTemplate to define the kind of panel which will hold your items, and set the ItemContainerTemplate to define how to draw each item.
If items should be drawn differently based on what type they are, I'd suggest using implicit DataTemplates instead of setting the ItemContainerTemplate
<Window.Resources>
<DataTemplate DataType="{x:Type my:BasePanelItem}">
<my:CustomPanelItem Param1="{Binding Param1}" Param2="{Binding Param2}" Content="{Binding SomeValue}" />
</DataTemplate>
<DataTemplate DataType="{x:Type my:SpecializedPanelItem}">
<my:SpecializedCustomPanelItem Content="{Binding SomeValue}" />
</DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding MyItems}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<my:CustomPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
You mentioned that you wanted to perhaps use a dynamically created Grid instead of a StackPanel as well. If you do, you might be interested in some GridHelpers I have posted on my blog. This would allow you to bind the number of Columns/Rows on the Grid in the ItemsPanelTemplate
<ItemsControl ItemsSource="{Binding MyCollection}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid local:GridHelpers.RowCount="{Binding RowCount}"
local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
<Setter Property="Grid.Row" Value="{Binding RowIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>

ElementName Binding is failing

I have the following XAML:
<Grid>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ...>
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
<DockPanel Grid.Row="2">
<CheckBox x:Name="DisplayMarkers" DockPanel.Dock="Top" Content="Display Data Points?"
Margin="8,5,0,5" d:LayoutOverrides="Height" HorizontalAlignment="Left" IsChecked="False" />
<vf:Chart DockPanel.Dock="Top" ScrollingEnabled="False" ZoomingEnabled="True" ToolBarEnabled="True">
<vf:DataSeries AxisYType="Secondary" RenderAs="Line" DataSource="{Binding CdTeRoughnessList}"
XValueType="DateTime"
MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}" Color="Navy"
LegendText="Roughness Std. Dev.">
This binding is failing: MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}"
I'm trying to bind to the IsChecked property on my Checkbox named 'DisplayMarkers". When I run this, in debug mode in VS 2010, the output window shows the binding is failing. It can't find the element named 'Checkbox'. Could anyone tell me why?
The error I'm getting from VS is:
System.Windows.Data Error: 4 : Cannot find source for binding with reference
'ElementName=DisplayMarkers'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'DataSeries' (Name=''); target property is 'MarkerEnabled' (type 'Nullable`1')
You might not have a namescope where you try to bind, you could try to replace the ElementName construct with Source={x:Reference DisplayMarkers}.
The gist of it is that if you have elements in XAML which are not in the visual or logical tree you will not be able to use certain bindings like RelativeSource and ElementName, I suspect that DataSeries is not in any tree either (it sure sounds like it's abstract).
For a workaround for potential cyclical dependency errors see: https://stackoverflow.com/a/6858917/546730
I'm guessing that the writer of Chart, when deriving from FrameworkElement or whatever, failed to realize that they needed to add any child elements to the logical tree either manually or through an override. You don't get that for free when deriving.
Breaking the logical tree breaks the ability of children to bind by ElementName.
If you are the author of the Chart object, you can see this related question and answer.
For other readers, another possible cause is using a UserControl instead of a custom control for what's in the role of vf:Chart, above. I wrote a split button (in the role of the chart) and changing it from a UserControl to a custom control got my ElementName binding working.

Are "{Binding Path=.}" and "{Binding}" really equal

In my WPF project, I have a ListBox that displays items from a List<string> collection. I wanted to make the text of these items editable, so I wrapped each of them in an ItemTemplate with a TextBox (might not be the best way, but I'm new to WPF). I was having trouble simply binding the TextBoxes' Text property to the value of each item. I finally stumbled upon an example using a single dot or period for its Path property ({Binding Path=.}):
<ListBox ItemsSource="{Binding ElementName=recipesListbox,Path=SelectedItem.Steps}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
However I don't understand why simply using {Binding} didn't work.
It raised a "Two-way binding requires Path or XPath" exception, as according to Microsoft:
[...] a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"
Could someone shed light on this ambiguous behavior?
EDIT: Moreover, it seems {Binding Path=.} does not necessarily give two-way binding, as modifying the text and moving the focus does not update the underlying source (the same source has also properties displayed and successfully modified on a DataGrid control). I'm definitely missing something here.
The point of the exception presumably is that you cannot two-way bind a binding-source itself, so it tries to prevent you from creating a binding which does not behave the way you would want it to. By using {Binding Path=.} you just trick the error detection.
(Also it's not unheard of that documentation is erroneous or inaccurate, though i do like the MSDN documentation a lot in general as it usually does contain the crucial points one is interested in)
The documentation states that {Binding} is equivalent to {Binding Path=.}. However it is not equivalent to {Binding Path} as you have typed. If you include the Path property, you must assign it to something, be it Path=. or Path=OtherProperty.
These are not the same. If you bind this where ConsoleMessages is an ObservableCollection string with just {Binding} you get a "Two-way binding requires Path or XPath." exception where as {Binding Path=.} works. This is with WPF 4.0...
<ItemsControl x:Name="ConsoleOutput" ItemsSource="{Binding ConsoleMessages, Mode=OneWay}" MaxHeight="400">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}" BorderThickness="0" Margin="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My 2p worth...
In short, the difference between the two is analogous with the difference between the traditional pass by value and pass by reference. (FYR - What's the difference between passing by reference vs. passing by value?)
However I don't understand why simply using {Binding} didn't work (it raised a "Two-way binding requires Path or XPath" exception)
Lets assume here for now that {Binding} can be used for two way binding. In general {Binding} creates a value based link with datacontext which does not allow updating the datacontext.
Whereas {Binding Path=.} creates reference based link with the memory area referenced by the 'Path' which allows updating the value through reference.(in this case 'dot' the current datacontext).
Hope this helps!

Resources