I do not understand the DataTemplate within the ItemTemplate. I have an ObservableCollection"StringViewModel" Transcription that provides the ItemsSource for an ItemsControl. Populating the Transcription collection with StringViewModel correctly displays these strings.
At the time of displaying each string, I would like the XAML binding to call MyConverter such that additional code can be run on each item being displayed. (I am not trying to change what is displayed, but only perform some actions based on the position of what is displayed).
In the following code, MyConverter is never called.
What is the best way to call MyConverter on each item presented in the ItemsControl?
Any help is appreciated.
C#
public class StringViewModel : FrameworkElement {...}
private ObservableCollection<StringViewModel> transcription = new ObservableCollection<StringViewModel>();
public ObservableCollection<StringViewModel> Transcription
{
get
{
return transcription;
}
set
{
transcription = value;
}
}
XAML
<ItemsControl
ItemsSource="{Binding Transcription}"
>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas
Background="Transparent"
Width="{x:Static h:Constants.widthCanvas}"
Height="{x:Static h:Constants.heightCanvas}"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate> <!-- THIS DOES NOT WORK -->
<ContentControl Content="{Binding Converter={StaticResource MyConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My attempt to change this to:
<ItemsControl.ItemTemplate>
<DataTemplate>
<StringViewModel ft="{Binding Path=., Converter={StaticResource MyConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
Results in:
ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='StringViewModel'
What to do?
The StringViewModel is defined as:
public class StringViewModel : FrameworkElement, INotifyPropertyChanged
{
public StringViewModel()
{
}
public StringViewModel(
Point topleft,
string text,
double fontsizediu,
SolidColorBrush brush,
Func<FormattedText,FormattedText> f,
double lineheight)
{
this.text = text;
this.emSize = fontsizediu;
this.color = brush;
this.topleft = topleft;
this.lineheight = lineheight;
this.f = f;
}
protected override void OnRender(DrawingContext dc)
{
ft = new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Segoe Script"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal),
emSize,
color);
ft.TextAlignment = TextAlignment.Left;
// apply special styles
ft = f(ft);
dc.DrawText(ft, topleft);
}
private string text;
private double emSize;
private SolidColorBrush color;
private Func<FormattedText, FormattedText> f;
public Point topleft;
private double? Lineheight;
private FormattedText _ft;
public FormattedText ft
{
get
{
return _ft;
}
set
{
if (_ft != value)
{
_ft = value;
OnPropertyChanged("ft");
}
}
}
<ItemsControl.ItemTemplate>
the Converter was never called? and with out<ItemsControl.ItemTemplate>
, it showsTranscription
in Canvas correctly? but i write a simple example, it works. see my temporary answer. – Rang