2016-11-16 4 views
0

1つのウィンドウに1800行XAML定義を使用する必要があり、コードの量を大幅に削減したいと考えています。統合されたテキストボックスでボーダースタイルを作成

非常に頻繁に繰り返されるいくつかのコントロール定義があり、その中にはいくつかのスタイルを記述したいと思います。一例は、統合されたテキストボックスとボーダーの定義である:バインディングパスのほかに

<Border Grid.Column="2" Margin="1,1,5,0" Background="#bbc2ce"> 
    <my:RibbonTextBox HorizontalContentAlignment="Center" 
         IsReadOnly="True" Background="#FAFAFA" 
         Text="{Binding Path=someViewModel.Item,Mode=OneWay}" 
         MinHeight="0" FontSize="12" FontWeight="Bold" FontFamily="Arial"/> 
</Border> 

、すべての値は、まったく同じ何度も何度もあります。だから私はRibbonTextBoxために、このスタイルを書いた:

<Style TargetType="my:RibbonTextBox" x:Key="StandardRibbonTextBox"> 
    <Setter Property="HorizontalAlignment" Value="Center" /> 
    <Setter Property="IsReadOnly" Value="True" /> 
    <Setter Property="Background" Value="#FAFAFA" /> 
    <Setter Property="MinHeight" Value="0" /> 
    <Setter Property="FontSize" Value="12" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
    <Setter Property="FontFamily" Value="Arial" /> 
</Style> 

は今、私は上記の境界線のスタイルを書き、RibbonTextBoxスタイルを統合したいです。ここまでです。

<Style TargetType="Border" x:Key="borderStyle"> 
    <Setter Property="Background" Value="#bbc2ce" /> 
</Style> 

ここにTextBoxスタイルを統合する可能性はありますか?そうでない場合、誰かがこの問題を解決する方法を知っていますか?

ありがとうございます!あなただけの読み取り専用テキストボックスが必要な場合は

+1

いいえ「Border.Child = TextBox」は、親のコントロールテンプレートを再定義しない限り、ネストされたコントロールに対して異なるバインディングを設定することはできません。いずれかの2つのスタイルを作成します。コントロールの繰り返し可能なレイアウト(グループ)を使用する必要がある場合は、 'UserControl'の使用を検討してください。 – Sinatr

+0

@Sinatr:ありがとう! – Joshit

答えて

0

、あなたは下のコード化された(デモンストレーション用のシンプルなTextBoxmy:RibbonTextBoxを置き換え)としてContentPresenterDataTemplateを使用することができます。読み書きが必要な場合は、読み書き可能なバインド方法を提供する必要があります。これはControlTemplate(サンプルコードも同様)で実現できます。

<Grid x:Name="grid1"> 
    <Grid.Resources> 

     <Style TargetType="TextBox" x:Key="StandardRibbonTextBox"> 
      <!--Changed that one from HorizontalAlignment, in comparison to the question--> 
      <Setter Property="HorizontalContentAlignment" Value="Center" /> 
      <Setter Property="IsReadOnly" Value="True" /> 
      <Setter Property="Background" Value="#FAFAFA" /> 
      <Setter Property="MinHeight" Value="0" /> 
      <Setter Property="FontSize" Value="12" /> 
      <Setter Property="FontWeight" Value="Bold" /> 
      <Setter Property="FontFamily" Value="Arial" /> 
     </Style> 

     <Style TargetType="Border" x:Key="borderStyle"> 
      <Setter Property="Background" Value="#bbc2ce" /> 
      <Setter Property="Margin" Value="1,1,5,0"/> 
      <Setter Property="Padding" Value="5"/> 
     </Style> 

     <DataTemplate x:Key="borderedTextboxTemplate" DataType="{x:Type sys:String}"> 
      <Border Style="{StaticResource borderStyle}"> 
       <!--This is not going to work for two way binding (IsReadOnly="False" and Text="{Binding Mode=TwoWay}")--> 
       <TextBox Style="{StaticResource StandardRibbonTextBox}" Text="{Binding Mode=OneWay}"/> 
      </Border> 
     </DataTemplate> 

     <ControlTemplate x:Key="borderedTextboxControlTemplate" TargetType="TextBox"> 
      <Border Style="{StaticResource borderStyle}"> 
       <!--This is not going to work for two way binding (IsReadOnly="False" and Text="{Binding Mode=TwoWay}")--> 
       <TextBox Style="{StaticResource StandardRibbonTextBox}" Text="{Binding Text,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/> 
      </Border> 
     </ControlTemplate> 

     <Style x:Key="borderedTextboxControlStyle" TargetType="TextBox"> 
      <Setter Property="Template" Value="{StaticResource borderedTextboxControlTemplate}"/> 
     </Style> 

    </Grid.Resources> 

    <StackPanel Margin="10"> 
     <!--Using Controls directly--> 
     <Border Style="{StaticResource borderStyle}"> 
      <TextBox 
       Text="{Binding Path=someViewModel.Item,Mode=OneWay}" 
       Style="{StaticResource StandardRibbonTextBox}"/> 
     </Border> 
     <Separator Margin="10"/> 
     <!--Using DataTemplate--> 
     <ContentPresenter 
      Content="{Binding Path=someViewModel.Item,Mode=OneWay}" 
      ContentTemplate="{StaticResource borderedTextboxTemplate}"/> 
     <Separator Margin="10"/> 
     <!--Using ControlTemplate via Style--> 
     <TextBox Text="{Binding Path=someViewModel.Item,Mode=OneWay}" Style="{StaticResource borderedTextboxControlStyle}"/> 
    </StackPanel> 
</Grid> 
+0

ありがとうございました。ご迷惑をおかけして申し訳ありません! – Joshit

+0

@Joshitこれは私に驚きました、古いものをもう一度見に行きました;) – grek40

+0

確かに!!私はサポートに本当に感謝しています。実際には私のスタイルでは答えられません。答えの欠落はあまり良い理由がありませんでした。私はあなたが答えに入れた時間と努力を敬う必要があると感じました。サポートありがとうございます;) – Joshit

関連する問題