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

本文关键字:绑定 UserControl 标签 WPF | 更新日期: 2023-09-27 18:15:49

我希望能够将fc:ColorSlider绑定到我的主窗口中的值。XAML下面的代码是我的ColorSlider。XAML

我想将文本内容绑定到我的RedSlider或RGB slider。通常情况下,这很容易,但我的滑块在UserControl中,我的文本框在主窗口中。

 <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>

这个代码是我的主窗口。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>

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

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

你应该能够从你的主窗口绑定到它