Silverlight,通过用户控件的模板绑定的路径

时间:2012-07-03 14:40:11

标签: silverlight binding dependency-properties templatebinding

我有一个带有TextBox(Binding TextBoxText)的MainPage.xaml和一些嵌套的UserControls,它们都有一个带有TemplateBinding / Binding的TextBox(4个TextBoxes)。
当我在某处更改文本时,我想在每个框中都有相同的文字。

解决方案:Link

如何同步所有文本?

谢谢!

MainPage.xaml(TextBoxText是我喜欢到处显示的那个)

<myContDll:DefaultContainerTemplate Caption="NewCaption">
    <StackPanel x:Name="myStackPanel">
        <sdk:Label Content="Binding TextBoxText of MainPage"/>
        <TextBox Text="{Binding TextBoxText}"/>
        <myDll:MyButtonControl x:Name="ButtonControl" Count="44"
                           Button3Caption="NewButton3" 
                           Button3Visibility="Visible"  
                           DisplayText="Form2Text2"
                           TextBoxText="{Binding TextBoxText}"/>
    </StackPanel>
</myContDll:DefaultContainerTemplate>

Generic.xaml:

<Style TargetType="local:MyButtonControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MyButtonControl">
                <StackPanel>
                    <sdk:Label Content="inner control test"/>
                    <sdk:Label Content="TemplateBinding TextBoxText of MyButtonControl"/>
                    <TextBox Text="{TemplateBinding TextBoxText}"/>
                    <Button Content="{TemplateBinding Button3Caption}" Visibility="{TemplateBinding Button3Visibility}" x:Name="Button3"/>
                    <local:CountDisplayControl x:Name="Control1" TextBoxText="{TemplateBinding TextBoxText}" Visibility="{TemplateBinding VisibilityText1}" Count="{TemplateBinding Count}" DisplayText="{TemplateBinding DisplayText1}"/>
                    <local:CountDisplayControl2 x:Name="Control2" TextBoxText="{TemplateBinding TextBoxText}" Visibility="{TemplateBinding VisibilityText2}" Count="{TemplateBinding Count}" DisplayText="{TemplateBinding DisplayText2}"/>
                    <local:CountDisplayUserControl TextBoxText="{TemplateBinding TextBoxText}"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="local:CountDisplayControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CountDisplayControl">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>
                        <sdk:Label Content="Control1"/>
                        <sdk:Label Content="{TemplateBinding Count}"/>
                        <sdk:Label x:Name="labelText" Content="{TemplateBinding Text}"/>
                        <sdk:Label Content="TemplateBinding TextBoxText of CountDisplayControl"/>
                        <TextBox Text="{TemplateBinding TextBoxText}"/>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="local:CountDisplayControl2">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CountDisplayControl2">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>
                        <sdk:Label Content="Control2"/>
                        <sdk:Label Content="{TemplateBinding Count}"/>
                        <sdk:Label x:Name="labelText" Content="{TemplateBinding Text}"/>
                        <sdk:Label Content="TemplateBinding TextBoxText of CountDisplayControl2"/>
                        <TextBox Text="{TemplateBinding TextBoxText}"/>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

尝试将Binding Mode设置为TwoWay绑定:

<myContDll:DefaultContainerTemplate Caption="NewCaption">
    <StackPanel x:Name="myStackPanel">
        <sdk:Label Content="Binding TextBoxText of MainPage"/>
        <TextBox Text="{Binding TextBoxText, Mode=TwoWay}"/>
        <myDll:MyButtonControl x:Name="ButtonControl" Count="44"
                    Button3Caption="NewButton3" 
                    Button3Visibility="Visible"  
                    DisplayText="Form2Text2"
                    TextBoxText="{Binding TextBoxText, Mode=TwoWay}"/>
    </StackPanel>
</myContDll:DefaultContainerTemplate>