目次

キーワード

依存プロパティ


XAML  XML  使

WPF  dependency property XAML  CLR 

使 DependencyObject  DependencyObject SetValue  GetValue 
object val = GetValue(DependencyPropertyIdentifier);
SetValue(DependencyPropertyIdentifier, val);

DependencyPropertyIdentifier  DependencyProperty   DependencyProperty  static readonly  TextBlock  Text 
public class TextBlock
{
  public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(TextBlock));
}

便  CLR 
public class TextBlock : DependencyObject
{
  public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(TextBlock));

  public string Text
  {
    get { return (string)this.GetValue(TextProperty); }
    set { this.SetValue(TextProperty, value); }
  }
}

XAML 
<TextBlock Name="textBlock" Text="テキスト" />

 Text  TextProperty 
textBlock.SetValue(TextProperty, "テキスト");


attached property

Grid 使 Grid  Grid.Row  Grid.Column 使
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >
  <Grid Width="120" Height="120" Background="Black">
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>

    <Border Grid.Row="0" Grid.Column="0"
      Width="50" Height="50" Background="Red"/>
    <Border Grid.Row="0" Grid.Column="1"
      Width="50" Height="50" Background="Green"/>
    <Border Grid.Row="1" Grid.Column="0"
      Width="50" Height="50" Background="Blue"/>
    <Border Grid.Row="1" Grid.Column="1"
      Width="50" Height="50" Background="Yellow"/>
  </Grid>
</Page>

4Border2×2 4 Border  Grid 

使



Attribute Syntax 使 Property Element Syntax 使

1使 使

使 Resources 

<Page> 
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <LinearGradientBrush x:Key="brush1"
      StartPoint="0, 0" EndPoint="1, 1">
      <GradientStop Color="Violet" Offset="0.0" />
      <GradientStop Color="Coral" Offset="1.0" />
    </LinearGradientBrush>
  </Page.Resources>
</Page>

x:Key 

Attribute Syntax 
<object property="{StaticResource key}" .../>

Property Element Syntax 
<object>
  <object.property>
    <StaticResource ResourceKey="key" .../>
  </object.property>
</object>


<TextBlock Background="{StaticResource brush1}" Text="textblock 1"/>
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <LinearGradientBrush x:Key="brush1"
      StartPoint="0, 0" EndPoint="1, 1">
      <GradientStop Color="Violet" Offset="0.0" />
      <GradientStop Color="Coral" Offset="1.0" />
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="brush2"
      StartPoint="0, 0" EndPoint="1, 1">
      <GradientStop Color="Turquoise" Offset="0.0" />
      <GradientStop Color="Gainsboro" Offset="1.0" />
    </LinearGradientBrush>
  </Page.Resources>

  <StackPanel Orientation="Vertical">
    <TextBlock Background="{StaticResource brush1}" Text="textblock 1"/>
    <TextBlock Background="{StaticResource brush2}" Text="textblock 2"/>
    <Button    Background="{StaticResource brush1}" Content="button 1"/>
    <TextBox   Background="{StaticResource brush2}" Text="textbox 1"/>
  </StackPanel>
</Page>


Resources  ResourceDictionary  ResourceDictionary  Source   XAML 

 XAML  StyleForLabel.xaml 
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >
  <Style TargetType="{x:Type Label}">
    <Setter Property="Background" Value="#eeeeff"/>
  </Style>
</ResourceDictionary>

 XAML  StyleForLabel.xaml 
<WrapPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >
  <WrapPanel.Resources>
    <ResourceDictionary Source="StyleForLabel.xaml"/>
  </WrapPanel.Resources>

  <Label Content="label 1"/>
  <Label Content="label 2"/>
  <Label Content="label 3"/>
</WrapPanel>



MergedDictionaries 
<WrapPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >
  <WrapPanel.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="StyleForButton.xaml"/>
        <ResourceDictionary Source="StyleForLabel.xaml"/>
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </WrapPanel.Resources>

  <Button Content="button 1"/>
  <Label Content="label 1"/>
  <Button Content="button 2"/>
  <Label Content="label 2"/>
  <Button Content="button 3"/>
  <Label Content="label 3"/>
</WrapPanel>

 

ResourceDictionary.xaml  

StyleForButton.xaml

StyleForLabel.xaml 使 WPF


Attribute Syntax 使 XAML markup extension

使 使 StaticResource  StaticResource  StaticResourceExtension 
<TextBlock
  Name="textblock1"
  Background="{StaticResource brush1}"
  Text="textblock 1"/>


StaticResourceExtension ex = new StaticResourceExtension();
ex.ResourceKey = "brush1";

TextBlock textblock1 = new TextBlock();
textblock1.Background = (Brush)ex.ProvideValue(serviceProvider);
textblock1.Text       = "textblock 1";


StaticResourceExtension  Page.Resources  brush1 

StaticResourceExtension   ResourceKey 1  {StaticResource ResourceKey=brush1}  {StaticResource brush1} 

StaticResource  DynamicResource  Binding  MarkupExtension  MarkupExtension  XAML 

StaticResource  Binding   WPF 


 Foreground  Background  style使

XAML HTML  CSS  HTML <p>  18pt  CSS  p {font-size: 18pt;}   <p>  CSS  p.footnote {font-size: 10pt;}  HTML  <p class="footnote"> 

XAML 
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontSize="18pt">
  <Page.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Blue" />
      <Setter Property="FontFamily" Value="Times New Roman"/>
      <Setter Property="FontStyle" Value="Italic"/>
    </Style>
  </Page.Resources>

  <StackPanel Orientation="Vertical">
    <TextBlock Text="text block 1"/>
    <Button Content="button 1"/>
    <TextBlock Text="text block 2"/>
    <Button Content="button 2"/>
    <TextBlock Text="text block 3"/>
  </StackPanel>
</Page>

 Style  Style  TargetType  Style  x:Key 

Style Setter   TextBlock Times New Roman 

Style  x:Key  Style 
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontSize="18pt">
  <Page.Resources>
    <Style TargetType="TextBlock" x:Key="em">
      <Setter Property="Foreground" Value="Blue" />
      <Setter Property="FontFamily" Value="Times New Roman"/>
      <Setter Property="FontStyle" Value="Italic"/>
    </Style>
  </Page.Resources>

  <StackPanel Orientation="Vertical">
    <TextBlock Style="{StaticResource em}" Text="text block 1"/>
    <Button Content="button 1"/>
    <TextBlock Text="text block 2"/>
    <Button Content="button 2"/>
    <TextBlock Text="text block 3"/>
  </StackPanel>
</Page>


<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontSize="18pt">
  <Page.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="FontFamily" Value="Times New Roman"/>
      <Setter Property="FontStyle" Value="Italic"/>
    </Style>
    <Style TargetType="TextBlock" x:Key="em">
      <Setter Property="Foreground" Value="Blue" />
      <Setter Property="FontWeight" Value="Bold" />
    </Style>
  </Page.Resources>

  <StackPanel Orientation="Vertical">
    <TextBlock Style="{StaticResource em}" Text="text block 1"/>
    <Button Content="button 1"/>
    <TextBlock Text="text block 2"/>
    <Button Content="button 2"/>
    <TextBlock Text="text block 3"/>
  </StackPanel>
</Page>

text block 1  Times New Roman 

Style  BasedOn  BasedOn x:Key  TargetType 
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontSize="18pt">
  <Page.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="FontFamily" Value="Times New Roman"/>
      <Setter Property="FontStyle" Value="Italic"/>
    </Style>
    <Style BasedOn="{StaticResource {x:Type TextBlock}}"
      TargetType="TextBlock" x:Key="em">
      <Setter Property="FontWeight" Value="Bold" />
    </Style>
    <Style BasedOn="{StaticResource em}"
      TargetType="TextBlock" x:Key="emred">
      <Setter Property="Foreground" Value="Red" />
    </Style>
  </Page.Resources>

  <StackPanel Orientation="Vertical">
    <TextBlock Style="{StaticResource em}" Text="text block 1"/>
    <Button Content="button 1"/>
    <TextBlock Style="{StaticResource emred}" Text="text block 2"/>
    <Button Content="button 2"/>
    <TextBlock Text="text block 3"/>
  </StackPanel>
</Page>

 Times New Roman  text block 1 Times New Roman  text block 2 Times New Roman 

更新履歴

ブログ