Results 1 to 3 of 3

Thread: [RESOLVED] [WPF/C#] Xaml Events

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Resolved [RESOLVED] [WPF/C#] Xaml Events

    Hey everyone, I ran into a small snag in my most recent project. I'm new to WPF (as well as xaml) and I'm having an issue with overriding the OnMouseLeftButtonDown event in my control.

    The way it is set up is this:
    default grid holds 'PART_StackPanel'
    which holds 1 column and 'Part_DateLabel'
    Here's the xaml:
    Code:
    <ControlTemplate
            x:Key="CtrlTemplate"
            TargetType="ButtonBase">
            <Border
                x:Name="ContentContainer"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="1"
                SnapsToDevicePixels="True">
                <ContentPresenter
                     x:Name="Content"
                     RecognizesAccessKey="True"
                     Content="{TemplateBinding Content}"
                     ContentTemplate="{TemplateBinding ContentTemplate}"
                     ContentStringFormat="{TemplateBinding ContentStringFormat}"
                     Margin="{TemplateBinding Padding}"
                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="UIElement.IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground">
                        <Setter.Value>
                            <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    
    <Style x:Key="{x:Type controls:CustomControl}" TargetType="{x:Type controls:CustomControl}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="75"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="0.25"/>
            <Setter Property="Template">            
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type controls:CustomControl}">
                        <Border
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
    
                                <StackPanel
                                    x:Name="PART_StackPanel"
                                    Orientation="Vertical" 
                                    VerticalAlignment="Stretch"
                                    HorizontalAlignment="Stretch">
                                    <Label
                                        x:Name="PART_DateLabel"
                                        Content="{TemplateBinding controls:CustomControl.LabelText}"
                                        HorizontalContentAlignment="Right"
                                        HorizontalAlignment="Stretch"/>
                                </StackPanel>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    The problem is that when I override the OnMouseLeftButtonDown event in the cs file like so:
    Code:
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
     	         base.OnMouseLeftButtonDown(e);
    
                 MessageBox.Show("MouseDown fired");
            }
    However, the event is only called when the user clicks on the label at the top of the control. The rest of the control is empty and so the 'clickable' area of the control seems to only extend as far as the Label.

    How can I set up the c# or xaml to call the OnMouseLeftButtonDown event throughout the entire control? Any ideas are much appreciated! Thanks!
    Last edited by silentB4dawn; Feb 8th, 2011 at 09:54 AM.

  2. #2
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: [WPF/C#] Xaml Events

    i'm not sure if this is your issue but i have had similar issues and i had to specifically set the background of the container in the control to transparent. your issue might be something similar, im not sure what you Background brush looks like.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Re: [WPF/C#] Xaml Events

    Quote Originally Posted by bflosabre91 View Post
    i'm not sure if this is your issue but i have had similar issues and i had to specifically set the background of the container in the control to transparent. your issue might be something similar, im not sure what you Background brush looks like.
    Hm, that did the trick! Strange

    Thanks!

    Why would that be?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width