试图从父控件获取滑块的值

时间:2010-01-29 17:15:40

标签: wpf xaml slider

我正在尝试从包含在该窗口中的用户控件中获取窗口中包含的滑块的值。

这就是我想要完成的事情。

<Window x:Class="TestApp3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">
    <Window.Resources>

        <Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
            <Setter Property="Value" Value="10" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Interval" Value="1" />
            <Setter Property="Minimum" Value="5" />
            <Setter Property="Maximum" Value="50" />
            <Setter Property="TickFrequency" Value="0.25" />
            <Setter Property="IsSnapToTickEnabled" Value="True" />
            <Setter Property="Width" Value="100" />
        </Style>

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
        </Style>

    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" Text="Test" />
        <Border
            Grid.Row="1"
            Background="Purple" 
            BorderBrush="Black"
            BorderThickness="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label
                    Grid.Column="0"
                    FontSize="16"
                    Content="Font Size:"/>
                <TextBox 
                    Grid.Column="1"
                    FontSize="16"
                    Text="{Binding ElementName=SliderFont, Path=Value, Mode=TwoWay}"
                    Width="50"
                    MaxLength="5" />
                <Slider 
                    Style="{DynamicResource SliderStyle}"
                    Grid.Column="2"
                    Name="SliderFont" />
            </Grid>
        </Border>
    </Grid>
</Window>

同样的想法,但使用用户控件。

<Window x:Class="TestApp3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestApp3"
    Title="Window1">
    <Window.Resources>

        <Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
            <Setter Property="Value" Value="10" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Interval" Value="1" />
            <Setter Property="Minimum" Value="5" />
            <Setter Property="Maximum" Value="50" />
            <Setter Property="TickFrequency" Value="0.25" />
            <Setter Property="IsSnapToTickEnabled" Value="True" />
            <Setter Property="Width" Value="100" />
        </Style>

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
        </Style>

    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <!--<TextBox Grid.Row="0" Text="Test" />-->
        <local:myusercontrol Grid.Row="0" />
        <Border
            Grid.Row="1"
            Background="Purple" 
            BorderBrush="Black"
            BorderThickness="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label
                    Grid.Column="0"
                    FontSize="16"
                    Content="Font Size:"/>
                <TextBox 
                    Grid.Column="1"
                    FontSize="16"
                    Text="{Binding ElementName=SliderFont, Path=Value, Mode=TwoWay}"
                    Width="50"
                    MaxLength="5" />
                <Slider 
                    Style="{DynamicResource SliderStyle}"
                    Grid.Column="2"
                    Name="SliderFont" />
            </Grid>
        </Border>
    </Grid>
</Window>

usercontrol

<UserControl x:Class="TestApp3.myusercontrol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TextBox Text="Test" />
    </Grid>
</UserControl>

usercontrols textbox fontsize根本没有增长。我想让这个工作的原因是因为我想在我们的主题中加入类似的东西,所以我们以后不必担心它。我一直在修补这个问题太久了。关于如何使这项工作的任何想法都会很棒。

我知道我可以传递usercontrol中的FontSize值,但我希望能够控制多个控件FontSize。

希望这是有道理的, 〜靴

2 个答案:

答案 0 :(得分:0)

好的,我花了一点时间,但我终于有了这个工作。

您需要在用户控件中创建一个依赖项属性(这在后面的代码中 - 在这种情况下是C#):

    public static readonly DependencyProperty UCFontSizeProperty = DependencyProperty.Register(
      "UCFontSize", typeof(double), typeof(myusercontrol));

    public double UCFontSize
    {
        get { return (double)this.GetValue(UCFontSizeProperty); }
        set { this.SetValue(UCFontSizeProperty, value); }
    }

然后在用户控件XAML中有:

<UserControl x:Class="TestApp3.myusercontrol"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="TextBoxUserControl"
    Height="200" Width="250">
    <Grid>
        <TextBox Text="Test" FontSize="{Binding ElementName=TextBoxUserControl, Path=UCFontSize}" x:Name="UCTextBox" />
    </Grid>
</UserControl>

然后添加另一个Style setter:

<Style TargetType="{x:Type local:U myusercontrol}">
    <Setter Property="UCFontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
</Style>

您无需更改主页上实例化用户控件的方式。

答案 1 :(得分:0)

我使用XMLfile作为资源让它工作。

只需将此添加到您的资源

即可
<XmlDataProvider x:Key="XmlFontFile" Source="pack://application:,,,/TestApp3;component/XMLFile1.xml" />

这是你的SliderStyle

<Setter Property="Value" Value="{Binding Source={StaticResource XmlFontFile}, XPath=Style/TextBoxFontSize, Mode=TwoWay}" />

这是你的TextBoxStyle

<Setter Property="FontSize" Value="{Binding Source={StaticResource XmlFontFile}, XPath=Style/TextBoxFontSize}" />

我的xmlfile看起来像

<?xml version="1.0" encoding="utf-8" ?>

&LT;样式和GT;   &LT; TextBoxFontSize&GT; 16&LT; / TextBoxFontSize&GT; &LT; /样式和GT;