질문하기질문하기
 

답변됨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일 화요일 오전 5: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일 화요일 오전 5: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