MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > Custom control problems(EDIT)
發問發問
 

已答覆Custom control problems(EDIT)

  • 2007年12月9日 下午 12:14w0lfshad3 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

     

    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.

解答

  • 2007年12月11日 上午 05:58Wei Zhou - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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

所有回覆

  • 2007年12月11日 上午 05:58Wei Zhou - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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