views:

42

answers:

0

I have 2 custom controls on my MainWindow.xaml. The first is an usercontrol that is a button/label combination (StyledButton) and the second is a custom textbox.

I need to be able to set the text on a StyledButton so I created a TextCaption property. The binding on the TextCaption is causing the Text property of my custom textbox to be blank.

You can see my problem by entering text into the custom textbox and then clicking the button. The messagebox should display the custom textbox text but is instead blank. If the TextCaption property was used, that text would be shown even if totally different text was entered into the box.

For anyone wanting a better understanding then just the code, I have uploaded the example project to here

MainWindow.xaml

<Grid>
    <Grid.RowDefinitions >
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        <my1:TextBoxControl x:Name="Test" Width="80" Height="50"  />
    </Grid>
    <Grid Grid.Row="1">
        <my:StyledButton Height="53" TextCaption="OK" Width="100" Click="Button_Click"/>
    </Grid>

</Grid>

StyledButton.xaml

<Grid>
    <Label Name="BackgroundLabel" BorderThickness="0" Panel.ZIndex="1" Cursor="Hand">
        <Label.Foreground>
            <SolidColorBrush Color="Black"/>
        </Label.Foreground>
    </Label>
    <TextBlock Name="ContentLabel" Text="{Binding TextCaption, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, FallbackValue='Styled Button'}" 
               TextAlignment="{Binding textAlignment, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, FallbackValue='Left'}"
               VerticalAlignment="Center" Margin="20,0,20,0" 
               FontSize="30" 
               Foreground="{Binding ElementName=BackgroundLabel, Path=Foreground}" />
</Grid>

StyledButton.cs - The important line, everything else works

public String TextCaption { get; set; }

TextBoxControl - Converter works perfectly

<control:TextBoxConverter x:Key="textboxConverter" />

<!-- Base Main Border Style-->
<Style x:Key="MainBorderBase" TargetType="{x:Type Border}">
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="BorderBrush" Value="#FF115394" />
    <Setter Property="Border.Effect">
        <Setter.Value>
            <DropShadowEffect Direction="315"  BlurRadius="10" Color="Black" Opacity="1"  />
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type control:TextBoxControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type control:TextBoxControl}">
                <Grid>
                    <TextBox FontFamily="Arial" FontSize="26" Foreground="Green" 
                             MaxHeight="35"
                             Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay}"
                    >
                    </TextBox>
                    <Border Width="{Binding ElementName=CustomTextBox, Path=ActualWidth, Converter={StaticResource textboxConverter}}"
                            Height="{Binding ElementName=CustomTextBox, Path=ActualHeight, Converter={StaticResource textboxConverter}}"
                            Style="{StaticResource MainBorderBase}" Panel.ZIndex="99"
                            Margin="0 1 0 1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

MainWindow.cs - Click event bringing back an empty string

MessageBox.Show(this.Test.Text);