2013-05-07 53 views
0

我有两个需要绑定的属性。一个是Value,它是UserControl的属性,第二个是来自图像元素的属性。图像元素在Window中。该窗口隐藏在后面的代码(UserControl)中。元素在后面的代码中实例化时绑定

我需要添加什么才能在图像src和属性值之间启用适当的绑定。代码如下所示。

<UserControl x:Class="nnnn"> 

<UserControl.Resources> 
    <propSheet:BitmapConverter x:Key="bitmapConverter" /> 

    <Window x:Key="imagePopup" 
      Width="640" 
      Height="480"> 
     <Grid Background="LightGray"> 
      <Image Grid.Row="0" Source="{Binding Path=Value, Converter={StaticResource bitmapConverter}}" /> 

      <TextBlock Text="{Binding Path=Value}"></TextBlock> 

      <Button Grid.Row="1" 
        Width="25" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Bottom" 
        Click="OpenFile_Click"> 
       ... 
      </Button> 
     </Grid> 
    </Window> 

</UserControl.Resources> 

<Grid> 
    <Button Click="ViewImage_Click">View Image</Button> 

</Grid> 

+0

什么???这个XAML不起作用。你不能在'UserControl'中放置一个'Window' ...你想要做什么? – 2013-05-07 15:35:16

+1

它不在用户控制之内。它在用户控制的资源里面!这是一个很大的区别! – dajuric 2013-05-08 08:00:25

回答

1

你将有一个显示之前将其窗口的DataContext属性设置为用户控件实例:

private void ViewImage_Click(object sender, RoutedEventArgs e) 
{ 
    var window = Resources["imagePopup"] as Window; 
    window.DataContext = this; // the UserControl 
    window.Show(); 
} 
相关问题