locked
Event Setter and Control Template RRS feed

  • Question

  • Hello World, I recently learned how to use a single Event Handler for some 80 individual textboxes, now I need round textboxes so I must use a control template to accomplish this, unless there is some other way of creating round textboxes that I'm not aware of.  The problem now is to use both the Event Setter/Handler and a Control Template.

        <Window.Resources>
            <Style x:Key="TbStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TextBox_PreviewMouseLeftButtonDown"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Border Name="Border"  CornerRadius="24" Height="48" Width="48" BorderThickness="10" Padding="10" BorderBrush="BlanchedAlmond"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
    With the Template included in the above the textboxes are correct, but the event does not happen, with the Template removed the event happens as desired, but the textboxes are not round.  Obviously I need to come at this from a different perspective.  Thank you for any help you can give me!


    Nitromuse

    Monday, December 15, 2014 3:16 PM

Answers

  • The event handler should get invoked when you click on the border brush. For it to get fired also when you click inside the border you must set the its Background property to Transparent:

    <Style x:Key="TbStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
          <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TextBox_PreviewMouseLeftButtonDown"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Name="Border"  CornerRadius="24" Height="48" Width="48" BorderThickness="10" Padding="10" BorderBrush="BlanchedAlmond"
                        Background="Transparent"/>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>

    If there is no background set, the area is considered to belong to the window and that's why your event handler doesn't get invoked.

    Please remember to mark helpful posts as answer and/or helpful and please start a new thread if you have a new question.

    • Marked as answer by Nitromuse Monday, December 15, 2014 3:33 PM
    Monday, December 15, 2014 3:22 PM
  • That's a totally different question that should have been asked in a new thread. Please don't ask several questions in the same thread :)

    Anyway, for you to be able type text into the TextBox its template should contain a ScrollViewer element named "PART_ContentHost". You may also want to increase the size of the Border a bit so you can actually see the text:

        <Style x:Key="TbStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
          <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TextBox_PreviewMouseLeftButtonDown"/>
          <Setter Property="FontSize" Value="12"/>
          <Setter Property="AcceptsReturn" Value="True"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Name="Border"  CornerRadius="24" Height="108" Width="108" BorderThickness="10" Padding="10" BorderBrush="BlanchedAlmond"
                        Background="Transparent">
                  <ScrollViewer x:Name="PART_ContentHost" />
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
    

    Please remember to mark all helpful posts as answer and/or helpful.
    • Marked as answer by Nitromuse Thursday, December 18, 2014 4:21 PM
    Thursday, December 18, 2014 2:15 PM

All replies

  • The event handler should get invoked when you click on the border brush. For it to get fired also when you click inside the border you must set the its Background property to Transparent:

    <Style x:Key="TbStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
          <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TextBox_PreviewMouseLeftButtonDown"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Name="Border"  CornerRadius="24" Height="48" Width="48" BorderThickness="10" Padding="10" BorderBrush="BlanchedAlmond"
                        Background="Transparent"/>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>

    If there is no background set, the area is considered to belong to the window and that's why your event handler doesn't get invoked.

    Please remember to mark helpful posts as answer and/or helpful and please start a new thread if you have a new question.

    • Marked as answer by Nitromuse Monday, December 15, 2014 3:33 PM
    Monday, December 15, 2014 3:22 PM
  • Thanks Magnus, that almost seems too easy

    but IT WORKS!  Thanks again.


    Nitromuse

    Monday, December 15, 2014 3:35 PM
  • Hi Magnus, I'm back on this and have discovered that the text is not visible in the textboxes subject to this style, the textbox and border are visible, but not the actual text. Other textboxes' text under a different style is visible.

    I am trying to set most of the properties directly in the style as to not have to do so repetitively in all 80 boxes, and I do need the corner radius.

    I assume I need to set some other properties for the textbox, or border, and have tried many but to no avail.  Can you tell me what I'm missing.

    Thanks again!


    Nitromuse

    Wednesday, December 17, 2014 8:53 PM
  • That's a totally different question that should have been asked in a new thread. Please don't ask several questions in the same thread :)

    Anyway, for you to be able type text into the TextBox its template should contain a ScrollViewer element named "PART_ContentHost". You may also want to increase the size of the Border a bit so you can actually see the text:

        <Style x:Key="TbStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
          <EventSetter Event="PreviewMouseLeftButtonDown" Handler="TextBox_PreviewMouseLeftButtonDown"/>
          <Setter Property="FontSize" Value="12"/>
          <Setter Property="AcceptsReturn" Value="True"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Name="Border"  CornerRadius="24" Height="108" Width="108" BorderThickness="10" Padding="10" BorderBrush="BlanchedAlmond"
                        Background="Transparent">
                  <ScrollViewer x:Name="PART_ContentHost" />
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
    

    Please remember to mark all helpful posts as answer and/or helpful.
    • Marked as answer by Nitromuse Thursday, December 18, 2014 4:21 PM
    Thursday, December 18, 2014 2:15 PM