2010-01-29 67 views
1

我試圖從包含在窗口中的用戶控件獲取包含在窗口中的滑塊的值。試圖從父控制獲取滑塊的值

這是我想要完成的。

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

同樣的想法,但使用usercontrol。

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

的用戶控件文本字體大小的心不是成長在所有。我希望得到這個工作的原因是因爲我想在我們的主題中放入類似它的東西,所以我們稍後將不必擔心它。我一直在修補這個太久。任何關於如何使這項工作的想法都會很棒。

我知道我可以傳遞usercontrol中的FontSize值,但我希望能夠控制多個控件的FontSize。

希望這是有道理的, 〜靴

回答

0

OK,我花了一小會兒,但我終於得到了這個工作。

你需要在你的用戶控件創建一個依賴屬性(這是在後面的代碼 - 在這種情況下,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> 

你不需要改變你的實例用戶C在您的主頁上進行控制。

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

<樣式> <TextBoxFontSize/TextBoxFontSize> < /樣式>