如何以编程方式在动态边框内创建图像?

时间:2014-05-06 11:56:11

标签: wpf border

我正在尝试创建一个Border dynamicaly并尝试在其中添加Image。这可能吗?

C#:

private void AddToHistory()
{
    HistoryBorder.Visibility = System.Windows.Visibility.Visible;
    Border Br = new Border();
    Br.BorderBrush = Brushes.Black;   
    Br.BorderThickness = new Thickness(2) ;                
    Image tem = new Image();
    tem.Source = image1.Source;
    Br.Content = tem;   // ?? Error here.  
    StackHistory.Children.Add(Br);   
}

XAML:

<Grid Grid.Column="2" x:Name="History">
  <ScrollViewer>
    <Border x:Name="HistoryBorder" BorderThickness="4" CornerRadius="1" BorderBrush="#FFF2C683" Visibility="Hidden">
       <StackPanel x:Name="StackHistory" HorizontalAlignment="Left" VerticalAlignment="Top" >
       </StackPanel>
    </Border>
  </ScrollViewer>
</Grid>

1 个答案:

答案 0 :(得分:4)

您需要设置Border.Child而不是Content

Br.Child = tem;