如果我將一個.cs文件中定義的ValueConverter添加到ResourceDictionary中,並多次將其用作靜態資源,它會創建新實例還是隻使用同一個?ResourceDictionary中的ValueConverter是Singleton?
--------------------------------- ValueConverterDefinition ------------ -------------------
internal class DateTimeConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var date = (DateTime)value;
return date.Day;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
------------------------ --------- ResourceDictionary -------------------------------
<converter:DateTimeConverter x:Key="DateTimeConverter"></converter:DateTimeToSpecificFormatConverter>
<Style x:Key="ToolTipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border>
<Grid>
<TextBlock Foreground="Black">
<TextBlock.Text>
<Binding Path="StartDate" Converter="{StaticResource DateTimeConverter}"></Binding>
</TextBlock.Text>
</TextBlock>
<TextBlock Foreground="Black">
<TextBlock.Text>
<Binding Path="EndDate" Converter="{StaticResource DateTimeConverter}"></Binding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我不認爲人們會在這裏談論單身人士,因爲這通常是指涉及靜態實例的特定設計模式。 – 2012-03-01 01:59:43