MSDN > Home page del forum > Windows Presentation Foundation (WPF) > Custom control problems(EDIT)
Formula una domandaFormula una domanda
 

Con rispostaCustom control problems(EDIT)

  • domenica 9 dicembre 2007 12.14w0lfshad3 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

     

    I am making a custom control with a label that pops up on double click a textbox wich returns text to the label on double click, enter or lose focus. It also centers itself beneath another control.

     

    I got a couple of problems:

    1. The centering doesn't occur when the custom control tries to extend beyond the above control's left side, it continues to extend to the right instead of centering.

    2. When the textbox pops up it doesn't receive focus from the Focus() method or keyboard input, instead the user must push the textbox again for it to gain focus and keyboard input.

     

    Project download:

    http://files.filefront.com/WpfApplication13rar/;9222886;/fileinfo.html

     

    EDIT: Problem 2 solved with:

     

    Code Block

    void textBox_Loaded(object sender, RoutedEventArgs e)

    {

    textBox.Focus();

    textBox.Select(0, textBox.Text.Length);

    e.Handled = true;

    }

     

     

    Problem 1 isn't a real issue anymore because it's solved in my main app, however remains a curiosity.

Risposte

  • martedì 11 dicembre 2007 5.58Wei Zhou - MSFTModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Hi w0lfshad3

     

    I think that this is because the calculation formula is not right. The following example has similar situation which shows how to center the TextBox control.

     

    Code Block

    namespace ForumProjects

    {

        public partial class MainWindow : Window

        {

            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)

            {

                if (!this.TheButton.IsLoaded || !this.TheTextBox.IsLoaded) return;

     

                double buttonHoriCenter = this.TheButton.ActualWidth / 2 + Canvas.GetLeft(this.TheButton);

                double textBoxWidth = this.TheTextBox.ActualWidth;

                double textBoxLeft = buttonHoriCenter - textBoxWidth / 2;

                Canvas.SetLeft(this.TheTextBox, textBoxLeft);

            }

        }

    }

    <Window x:Class="ForumProjects.MainWindow"

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

           x:Name="TheWindow" Title="MainWindow" Height="600" Width="800">

        <Window.Resources>

        </Window.Resources>

     

        <Canvas Name="RootLayout" Background="AliceBlue">

            <Button Name="TheButton" Width="100" Height="100" Canvas.Left="50" Canvas.Top="50"/>

            <TextBox Name="TheTextBox" Canvas.Left="80" Canvas.Top="150" TextChanged="TextBox_TextChanged">TTTTTT</TextBox>

        </Canvas>

    </Window>

     

     

    Best Regards,

    Wei Zhou

Tutte le risposte

  • martedì 11 dicembre 2007 5.58Wei Zhou - MSFTModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Hi w0lfshad3

     

    I think that this is because the calculation formula is not right. The following example has similar situation which shows how to center the TextBox control.

     

    Code Block

    namespace ForumProjects

    {

        public partial class MainWindow : Window

        {

            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)

            {

                if (!this.TheButton.IsLoaded || !this.TheTextBox.IsLoaded) return;

     

                double buttonHoriCenter = this.TheButton.ActualWidth / 2 + Canvas.GetLeft(this.TheButton);

                double textBoxWidth = this.TheTextBox.ActualWidth;

                double textBoxLeft = buttonHoriCenter - textBoxWidth / 2;

                Canvas.SetLeft(this.TheTextBox, textBoxLeft);

            }

        }

    }

    <Window x:Class="ForumProjects.MainWindow"

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

           x:Name="TheWindow" Title="MainWindow" Height="600" Width="800">

        <Window.Resources>

        </Window.Resources>

     

        <Canvas Name="RootLayout" Background="AliceBlue">

            <Button Name="TheButton" Width="100" Height="100" Canvas.Left="50" Canvas.Top="50"/>

            <TextBox Name="TheTextBox" Canvas.Left="80" Canvas.Top="150" TextChanged="TextBox_TextChanged">TTTTTT</TextBox>

        </Canvas>

    </Window>

     

     

    Best Regards,

    Wei Zhou