tags:

views:

118

answers:

1

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

A: 

I think you're trying to do this the hard way, control templates support events (called triggers) see http://msdn.microsoft.com/en-us/library/ms750821%28VS.85%29.aspx

Update: Ok I think I see what your problem is, you're defining a style for type TextBox and then placing a TextBox in the style. You should still be able to combine all your styles into one by using Triggers

Example

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type TextBoxBase}">
      <Border
        Name="Border"
        CornerRadius="2"
        Padding="2"
        Background="{DynamicResource TextBoxBackBrush}"
        BorderBrush="{StaticResource SolidBorderBrush}"
        BorderThickness="1" >
        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
          <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}"/>
          <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>
          <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Setter.Value>
</Setter>

Thomas
I dont think this will work because I have a custom listbox of textboxes.
Ryan
How would I create a style that only changes the TextBox.Background. The only way I was able to figure this out, was by adding a brand new textbox in the style.
Ryan
Nevermind, I created a LinearGradientBrush and got that part working.
Ryan