Having such MarkupExtension
public class Extension1 : MarkupExtension
{
private static int _counter = 0;
public override object ProvideValue(IServiceProvider serviceProvider)
{
return string.Format("Item {0}", _counter++);
}
}
and this XAML
<ListBox>
<ListBoxItem Content="{my:Extension1}"></ListBoxItem>
<ListBoxItem Content="{my:Extension1}"></ListBoxItem>
<ListBoxItem Content="{my:Extension1}"></ListBoxItem>
</ListBox>
I get such list:
Item 1
Item 2
Item 3
Now I try to generate the same list using this Style
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<TextBox Text="{my:Extension1}"></TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And with such XAML
<ListBox ItemsSource="{StaticResource data}"></ListBox>
I get
Item 0
Item 0
Item 0
So {my:Extension1} evaluated only once. Can I create a computed property that will be evaluated for every item?