Jishan
Jishan

Reputation: 1684

TextBox Customization Malfunction

I needed to create a textBoox that completely blends with the background so I used this XAML:

<TextBox Text="Hello" VerticalAlignment="Bottom" Name="txtAsk" FontFamily="Consolas" Margin="8,0,0,0" TextWrapping="Wrap" AutoWordSelection="True" KeyDown="onEnter">
                <TextBox.Template>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <Rectangle  Stroke="{StaticResource ResourceKey=detailMarkBrush}" StrokeThickness="0"/>
                            <TextBox Margin="4" Text="{TemplateBinding Text}" BorderThickness="0" Background="{x:Null}" SnapsToDevicePixels="True" />
                        </Grid>
                    </ControlTemplate>
                </TextBox.Template>               
            </TextBox>

However, now the problem is, when I attached a button to the interface and triggered it to show the content of the TextBox in a message window, it only shows the default text even after changing the value inside the text box! The button code is as follows:

private void btnTriggerEnter_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(txtAsk.Text);
        }

I am not sure where I screwed up. Please comment if any other detail is required. My MainWindow has this code:

public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);         
        }

Thanks.

Upvotes: 0

Views: 60

Answers (1)

Colin Smith
Colin Smith

Reputation: 12530

You should use:

Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text}"

That's because TemplateBinding only gives you a OneWay source (in the underlying Binding), you need TwoWay.

Upvotes: 1

Related Questions