C#WPF:如何将标签的内容绑定到UserControl中的值

时间:2014-07-31 20:39:15

标签: c# wpf xaml

我希望能够将fc:ColorSlider绑定到MainWindow.XAML中的值 以下代码是我的ColorSlider.XAML

我想将文本内容绑定到我的RedSlider或RGB Sliders。通常情况下这很简单,但我的滑块位于UserControl中,而我的文本框位于我的MainWindow中。

 <Rectangle Width="165" HorizontalAlignment="Right" Grid.Column="0" Stroke="Black">
            <Rectangle.Fill>
                <SolidColorBrush>
                    <SolidColorBrush.Color>
                        <MultiBinding Converter="{StaticResource RgbConverter}">
                            <Binding Path="Value" ElementName="RedSlider"/>
                            <Binding Path="Value" ElementName="GreenSlider"/>
                            <Binding Path="Value" ElementName="BlueSlider"/>
                            <Binding Path="Value" ElementName="AlphaSlider"/>
                        </MultiBinding>
                        <!-- Describes the brush's color using
                              RGB values. Each value has a range of 0-255.  -->
                        <!--<Color A="255" R="255" G="0" B="0" />-->
                    </SolidColorBrush.Color>
                </SolidColorBrush>
            </Rectangle.Fill>
        </Rectangle>

此代码是我的MainWindow.XAML

<fc:ColorSlider Grid.Column="0" Grid.Row="8" Grid.ColumnSpan="2"  x:Name="ColorSlider">     </fc:ColorSlider>

    <TextBox Grid.Row="1" Grid.Column="0" Width="250" Height="Auto" Text="Hello" HorizontalAlignment="Center" Margin="6,0,2,0"></TextBox>
    <TextBox Grid.Row="2" Grid.Column="0" Width="250" Text="{Binding ElementName=ColorSlider, Path=RedSlider, Mode=TwoWay}" HorizontalAlignment="Center"></TextBox>
    <TextBox Grid.Row="3" Grid.Column="0" Width="250" Text="Hello" HorizontalAlignment="Center"></TextBox>
    <TextBox Grid.Row="4" Grid.Column="0" Width="250" Text="Hello" HorizontalAlignment="Center"></TextBox>


    <TextBox Grid.Row="1" Grid.Column="1" Width="250" Text="Hello" HorizontalAlignment="Center"></TextBox>
    <TextBox Grid.Row="2" Grid.Column="2" Width="250" Text="Hello" HorizontalAlignment="Center"></TextBox>
    <TextBox Grid.Row="3" Grid.Column="1" Width="250" Text="Hello" HorizontalAlignment="Center"></TextBox>
    <TextBox Grid.Row="4" Grid.Column="1" Width="250" Text="Hello" HorizontalAlignment="Center"></TextBox>

    <CheckBox Grid.Column="0" Grid.Row="5" HorizontalAlignment="Left" VerticalAlignment="Top">Display Preview With Sliders?</CheckBox>

1 个答案:

答案 0 :(得分:0)

在用户控件的代码后面创建一个属性。将控件滑块值绑定到该属性。

您也应该可以从主窗口绑定它。