Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WCF and WF
  4. How to get access to control in template from source code

How to get access to control in template from source code

Scheduled Pinned Locked Moved WCF and WF
questiondatabasewpfwcftutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ArtemCh
    wrote on last edited by
    #1

    Hi! I want to create editing ListBox. To do this I've defined control template for ListBoxItem:

        <Style x:Key="TextBoxListStyle" TargetType="{x:Type ListBox}">
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Margin" Value="2"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <TextBox Name="PART\_TextBox" BorderThickness="0" 
                                             Text="{Binding Path=Content, Mode=OneWay, Converter={StaticResource RecordNameGetter},
                                                    RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <ControlTemplate.Triggers>
                                        <Trigger SourceName="PART\_TextBox" Property="IsFocused" Value="True">
                                            <Setter Property="IsSelected" Value="True"/>
                                            <Setter TargetName="PART\_TextBox" Property="BorderThickness" Value="1"/>
                                        </Trigger>
                                        <Trigger SourceName="PART\_TextBox" Property="IsFocused" Value="False">
                                            <Setter Property="IsSelected" Value="False"/>
                                            <Setter TargetName="PART\_TextBox" Property="BorderThickness" Value="0"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    

    ...
    <ListBox Name="lstSomeList" Style="{StaticResource TextBoxListStyle}"
    SelectionChanged="OnSelectionChangeSomeList"/>

    Question is how can I get access to TextBox "PART_TextBox" defined in template from OnSelectionChangeSomeList method?

    K 1 Reply Last reply
    0
    • A ArtemCh

      Hi! I want to create editing ListBox. To do this I've defined control template for ListBoxItem:

          <Style x:Key="TextBoxListStyle" TargetType="{x:Type ListBox}">
              <Setter Property="ItemContainerStyle">
                  <Setter.Value>
                      <Style TargetType="{x:Type ListBoxItem}">
                          <Setter Property="Margin" Value="2"/>
                          <Setter Property="Template">
                              <Setter.Value>
                                  <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                      <TextBox Name="PART\_TextBox" BorderThickness="0" 
                                               Text="{Binding Path=Content, Mode=OneWay, Converter={StaticResource RecordNameGetter},
                                                      RelativeSource={RelativeSource TemplatedParent}}"/>
                                      <ControlTemplate.Triggers>
                                          <Trigger SourceName="PART\_TextBox" Property="IsFocused" Value="True">
                                              <Setter Property="IsSelected" Value="True"/>
                                              <Setter TargetName="PART\_TextBox" Property="BorderThickness" Value="1"/>
                                          </Trigger>
                                          <Trigger SourceName="PART\_TextBox" Property="IsFocused" Value="False">
                                              <Setter Property="IsSelected" Value="False"/>
                                              <Setter TargetName="PART\_TextBox" Property="BorderThickness" Value="0"/>
                                          </Trigger>
                                      </ControlTemplate.Triggers>
                                  </ControlTemplate>
                              </Setter.Value>
                          </Setter>
                      </Style>
                  </Setter.Value>
              </Setter>
          </Style>
      

      ...
      <ListBox Name="lstSomeList" Style="{StaticResource TextBoxListStyle}"
      SelectionChanged="OnSelectionChangeSomeList"/>

      Question is how can I get access to TextBox "PART_TextBox" defined in template from OnSelectionChangeSomeList method?

      K Offline
      K Offline
      koleraba
      wrote on last edited by
      #2

      Hi I'm not sure if this helps but maybe try creating a custom ListBox whick extends the one provided by the framework. Below is example with the button.

      <Button x:Class="TestApp.MyButton"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="300" Width="300">
      <Grid
      x:FieldModifier="private"
      Background="Transparent"
      IsHitTestVisible="True"
      >

         <Ellipse 
        x:Name="PART\_Bubble" 
        x:FieldModifier="public" 
        IsHitTestVisible="False"
        Width="30" Height="30" Fill="AliceBlue"
        />
      </Grid>
      

      </Button>

      Then you can access the inner Ellipse with the following code:

      MyButton button = new MyButton();
      button.PART_Bubble.Fill = new SolidColorBrush(Colors.Yellow);

      Uroš

      A 1 Reply Last reply
      0
      • K koleraba

        Hi I'm not sure if this helps but maybe try creating a custom ListBox whick extends the one provided by the framework. Below is example with the button.

        <Button x:Class="TestApp.MyButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
        <Grid
        x:FieldModifier="private"
        Background="Transparent"
        IsHitTestVisible="True"
        >

           <Ellipse 
          x:Name="PART\_Bubble" 
          x:FieldModifier="public" 
          IsHitTestVisible="False"
          Width="30" Height="30" Fill="AliceBlue"
          />
        </Grid>
        

        </Button>

        Then you can access the inner Ellipse with the following code:

        MyButton button = new MyButton();
        button.PART_Bubble.Fill = new SolidColorBrush(Colors.Yellow);

        Uroš

        A Offline
        A Offline
        ArtemCh
        wrote on last edited by
        #3

        Koleraba, thank you for reply. But i've already solved my problem. In first message i raised more comprehensive problem then it was. I've just needed to update ListBoxItem source when it loses focus, becouse binding which is defined in template is one way. To do this i simply wrote OnKeyBoardLostFocus handler for the TextBox. Template:

        <TextBox Name="PART_TextBox" BorderThickness="0"
        Text="{Binding Path=Content, Mode=OneWay,
        Converter={StaticResource RecordNameGetter},
        RelativeSource={RelativeSource TemplatedParent}}"
        LostKeyboardFocus="OnLTBLostKeybordFocus"/>

        Handler:

        private void OnLTBLostKeybordFocus(object sender, KeyboardFocusChangedEventArgs args)
        {
        TextBox tb_sender = (TextBox)sender;
        if (typeof(SomeClass1) == tb_sender.DataContext.GetType())
        {
        SomeClass1 ob1 = (SomeClass1)tb_sender.DataContext;
        ob1.Name = tb_sender.Text;
        }
        else if (typeof(SomeClass2) == tb_sender.DataContext.GetType())
        {
        SomeClass2 ob2 = (SomeClass2)tb_sender.DataContext;
        ob2.Name = tb_sender.Text;
        }
        }

        So initial problem was solved, but this solution is far from been good extensible. If i want use the template with another one SomeClass3 i should modify OnLTBLostKeybordFocus. And if i'll have access to the TextBox from code i can update ListBoxItem source in "any" place of the application. Solution proposed by you is fitted for control with content, but ListBox isn't such type of control. Therefore how does your solution work with ListBox is open for me question. And does solution for the problem outlined in my firs message exist is open question for me too...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups