2017-05-30 14 views
0

再利用ユーザーコントロールのヘッダーを含むXAMLからヘッダーを変更する方法を探しています。再利用されたUserControlでWPF GroupBoxヘッダーを変更する方法

ユーザーコントロール:

<UserControl x:Class="ReusableControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="150" d:DesignWidth="300"> 
<Grid> 
<GroupBox Header="Config Number"> 
    <Grid Name="MyConfig"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="60" /> 
      <RowDefinition Height="60" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="80" /> 
      <ColumnDefinition Width="*" MinWidth="100"/> 
     </Grid.ColumnDefinitions> 

     <Label Name="Value1Label" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left">Value 1</Label> 
      <TextBox Grid.Row="0" Grid.Column="1" 
      HorizontalContentAlignment="Left" VerticalContentAlignment="Center" 
      IsEnabled="True" Margin="5,5,5,5"> 
       <TextBox.Text> 
        <Binding Path="Value1" StringFormat="{}{0:0.0#######}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" /> 
       </TextBox.Text> 
     </TextBox> 


     <Label Name="Value2Label" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left">Value 2</Label> 
      <TextBox Grid.Row="1" Grid.Column="1" 
      HorizontalContentAlignment="Left" VerticalContentAlignment="Center" 
      IsEnabled="True" Margin="5,5,5,5"> 
      <TextBox.Text> 
       <Binding Path="Value2" StringFormat="{}{0:0.0#######}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" /> 
      </TextBox.Text> 
     </TextBox> 

    </Grid> 
</GroupBox> 

</Grid> 

含むウィンドウ:

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:StackExchangeQuestion" 
Title="MainWindow" Height="500" Width="525"> 

<Grid> 
    <StackPanel Orientation="Vertical"> 
     <local:ReusableControl x:Name="Config1" /> 
     <local:ReusableControl x:Name="Config2" /> 
     <local:ReusableControl x:Name="Config3" /> 
    </StackPanel> 
</Grid> 

enter image description here

意図があること連続するコントロールが「Config Number」ではなく「Config 1」、「Config 2」、「Config 3」と3回表示されるようにします。

ありがとうございました!あなたは今

<GroupBox Header="{Binding Header, 
      RelativeSource={RelativeSource AncestorType=local:ReusableControl}}"> 

public string Header 
{ 
    get { return (string)GetValue(HeaderProperty); } 
    set { SetValue(HeaderProperty, value); } 
} 
public static readonly DependencyProperty HeaderProperty = 
    DependencyProperty.Register("Header", 
     typeof(string), typeof(ReusableControl)); 

今、あなたは内部GroupBoxHeaderプロパティにバインディングのソースとしてこのHeaderプロパティを使用することができます:あなたのReusableControl.xaml.csこのようなDependencyPropertyを定義するには

答えて

2

これが表示されます:

<StackPanel Orientation="Vertical"> 
    <local:ReusableControl x:Name="Config1" Header="Config 1"/> 
    <local:ReusableControl x:Name="Config2" Header="Config 2"/> 
    <local:ReusableControl x:Name="Config3" Header="Config 3"/> 
</StackPanel> 
+0

ありがとう! xmlns:local行(ReusableControlに)を追加しなければならず、それが完全に機能しました。 – JulieC

関連する問題