Bind property of one instantiated control

image_pdfimage_print


   
   

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="460" Height="200"
  Title="Binding the Properties of Two Controls">
    <DockPanel>
      <TextBlock>Choose a Color:</TextBlock>
      <ComboBox Name="myComboBox" SelectedIndex="0">
        <ComboBoxItem>Green</ComboBoxItem>
        <ComboBoxItem>Blue</ComboBoxItem>
        <ComboBoxItem>Red</ComboBoxItem>
      </ComboBox>
      <Canvas>
        <Canvas.Background>
          <Binding ElementName="myComboBox" Path="SelectedItem.Content"/>
        </Canvas.Background>
      </Canvas>
    </DockPanel>
</Window>

   
    
    
     


Bind to Window itself

image_pdfimage_print


   
   

<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SizingPrecedenceSampleCSharp.SizingWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Title="Sizing Window" Name="window">
  <StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>WindowState:</Label>
      <Label Content="{Binding ElementName=window,Path=WindowState}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MinWidth:</Label>
      <Label Content="{Binding ElementName=window,Path=MinWidth}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MinHeight:</Label>
      <Label Content="{Binding ElementName=window,Path=MinHeight}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>SizeToContent:</Label>
      <Label Content="{Binding ElementName=window,Path=SizeToContent}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MaxWidth:</Label>
      <Label Content="{Binding ElementName=window,Path=MaxWidth}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>MaxHeight:</Label>
      <Label Content="{Binding ElementName=window,Path=MaxHeight}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Width:</Label>
      <Label Content="{Binding ElementName=window,Path=Width}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Height:</Label>
      <Label Content="{Binding ElementName=window,Path=Height}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Actual Width:</Label>
      <Label Content="{Binding ElementName=window,Path=ActualWidth}"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Actual Height:</Label>
      <Label Content="{Binding ElementName=window,Path=ActualHeight}"/>
    </StackPanel>
  </StackPanel>
</Window>

   
    
    
     


Two level path binding

image_pdfimage_print


   
   


<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Label Content="Angle" />
    <ScrollBar Name="angle" Orientation="Horizontal" Value="0" Minimum="0" Maximum="360" />
    <TextBlock HorizontalAlignment="Center" Text="{Binding ElementName=angle, Path=Value}" />

    <Label Content="CenterX" />
    <ScrollBar Name="xcenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" /> 
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=xcenter, Path=Value}" />

    <Label Content="CenterY" />
    <ScrollBar Name="ycenter" Orientation="Horizontal" Value="0" Minimum="-100" Maximum="100" />
    <TextBlock HorizontalAlignment="Center" Margin="12" Text="{Binding ElementName=ycenter, Path=Value}" />

    <Canvas>

        <Button Name="btn" Content="Button" Canvas.Left="100" Canvas.Top="100">
            <Button.RenderTransform>
                <RotateTransform
                    Angle="{Binding ElementName=angle, Path=Value}"
                    CenterX="{Binding ElementName=xcenter, Path=Value}"
                    CenterY="{Binding ElementName=ycenter, Path=Value}" />
            </Button.RenderTransform>
        </Button>

        <StackPanel>
            <TextBlock Text="{Binding ElementName=btn, Path=ActualWidth}" />
            <TextBlock Text="::" />
            <TextBlock Text="{Binding ElementName=btn, Path=ActualHeight}" />
        </StackPanel>
    </Canvas>
</StackPanel>

   
    
    
     


Bind RelativeSource's AncestorType

image_pdfimage_print


   
   
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            TextBlock.FontSize="12" >
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock Text="This TextBlock is inside a StackPanel with " />
        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Orientation}" />
        <TextBlock Text=" orientation" />
    </StackPanel>


</StackPanel>

   
    
    
     


Bind RelativeSource's AncestorType's Path

image_pdfimage_print


   
   


<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            TextBlock.FontSize="12" >
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}, AncestorLevel=2},Path=Orientation}" />

    </StackPanel>



</StackPanel>

   
    
    
     


Bind Stroke Thickness to Slider

image_pdfimage_print


   
   

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <Polyline Margin="0.5in, 1.5in, 0, 0" 
              Points="0 0, 500 25, 0 50"
              VerticalAlignment="Center"
              Stroke="Blue"
              StrokeThickness="{Binding ElementName=sliderThickness,Path=Value }"
    />
    <Label Content="_Thickness" />
    <Slider Name="sliderThickness"
                    Minimum="0"
                    Maximum="100"
                    Value="24" />

</StackPanel>