【问题标题】:Trying to get value of a slider from parent control试图从父控件中获取滑块的值
【发布时间】:2010-01-29 17:15:40
【问题描述】:

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

这就是我想要完成的。

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

用户控件文本框字体大小根本没有增长。我想让这个工作的原因是因为我想把类似的东西放在我们的主题中,这样我们以后就不用担心了。我一直在修补这个太久了。关于如何使这项工作发挥作用的任何想法都会很棒。

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

希望这是有道理的, ~靴子

【问题讨论】:

    标签: wpf xaml slider


    【解决方案1】:

    好的,我花了一点时间,但我终于搞定了。

    您需要在您的用户控件中创建一个依赖属性(这在后面的代码中 - 在本例中为 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 TargetType="{x:Type local:U myusercontrol}">
        <Setter Property="UCFontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
    </Style>
    

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

    【讨论】:

      【解决方案2】:

      我让它使用 XML 文件作为资源来工作。

      只需将其添加到您的资源中

      <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}" />
      

      我的 xml 文件看起来像

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

      16样式>

      【讨论】:

        猜你喜欢
        • 2015-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多