WPF Infinite loop in references found while processing the Template
- by Ryan
I am pretty new to WPF and am getting this error after my mouse is over my custom listbox item.
Error: Infinite loop in references found while processing the Template for an element named '' of type 'System.Windows.Controls.TextBox'.
<Window.Resources>
<ControlTemplate x:Key="MouseOverFocusTemplate" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FF6FB8FD"
FontSize="24"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF013B73" Offset="0.501"/>
<GradientStop Color="#FF091F34"/>
<GradientStop Color="#FF014A8F" Offset="0.5"/>
<GradientStop Color="#FF003363" Offset="1"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</ControlTemplate>
<Style x:Key="MouseOverFocusStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource MouseOverFocusTemplate}"/>
</Style>
<ControlTemplate x:Key="LostFocusTemplate" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FF6FB8FD"
FontSize="24"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5"/>
<SkewTransform CenterX="0.5" CenterY="0.5"/>
<RotateTransform CenterX="0.5" CenterY="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF091F34" Offset="1"/>
<GradientStop Color="#FF002F5C" Offset="0.4"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</ControlTemplate>
<Style x:Key="LostFocusStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource LostFocusTemplate}"/>
</Style>
<ControlTemplate x:Key="GotFocusTemplate" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FFE38E27"
FontSize="24"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0.501"/>
<GradientStop Color="#FF091F34"/>
<GradientStop Color="#FF002F5C" Offset="0.5"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</ControlTemplate>
<Style x:Key="GotFocusStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource GotFocusTemplate}"/>
</Style>
<Style TargetType="ListBoxItem">
<EventSetter Event="GotFocus" Handler="ListItem_GotFocus"></EventSetter>
<EventSetter Event="LostFocus" Handler="ListItem_LostFocus"></EventSetter>
<EventSetter Event="Mouse.MouseMove" Handler="ListItem_MouseOver"></EventSetter>
</Style>
<DataTemplate DataType="{x:Type TextBlock}">
</DataTemplate>
<DataTemplate x:Key="CustomListData" DataType="{x:Type ListBoxItem}">
<Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Grid.RenderTransform>
<!--<ScrollViewer x:Name="PART_ContentHost" />-->
<TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
Foreground="#FF6FB8FD"
FontSize="24"
FocusVisualStyle="{StaticResource GotFocusStyle}"
TextWrapping="Wrap"
Text="{Binding .}"
Grid.Column="1"
Grid.Row="1"
MinHeight="55"
Cursor="Hand"
IsReadOnly="True"
FontFamily="Arial"
>
<TextBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5"/>
<SkewTransform CenterX="0.5" CenterY="0.5"/>
<RotateTransform CenterX="0.5" CenterY="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FF091F34" Offset="1"/>
<GradientStop Color="#FF002F5C" Offset="0.4"/>
</LinearGradientBrush>
</TextBox.Background>
</TextBox>
</Grid>
</Border>
</DataTemplate>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
</Window.Resources>
<Window.DataContext>
<ObjectDataProvider
ObjectType="{x:Type local:ImageLoader}"
MethodName="LoadImages"
/>
</Window.DataContext>
<ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79">
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush>
</ListBox.Resources>
</ListBox>
The code behind for the mouse over event is as follows
private void ListItem_MouseOver(object sender, RoutedEventArgs e)
{
e.Handled = true;
FrameworkElement element = e.OriginalSource as FrameworkElement;
if (element != null)
{
while (VisualTreeHelper.GetParent(element) != null)
{
element = VisualTreeHelper.GetParent(element) as FrameworkElement;
TextBox item = element as TextBox;
if (item != null)
{
item.Style = (Style)item.FindResource("MouseOverFocusStyle");
return;
}
}
}
}
What am I missing? Is there an easier way to do this ?
Thanks in advance
Ryan