在contentpresenter的datatemplate中绑定图像

时间:2014-07-04 13:44:09

标签: c# wpf binding datatemplate contentpresenter

我有一个带有contentpresenter的wpf叠加层:

<Border Background="#2E000000" Name="VentanaEmergente" Visibility="Collapsed" Padding="100" Grid.RowSpan="2" MouseUp="VentanaEmergente_MouseUp">
        <Border Background="AliceBlue" VerticalAlignment="Center" HorizontalAlignment="Center" Width="345" Height="145">
            <Grid>
                <ContentPresenter Name="ContenidoVentanaEmergente" />
            </Grid>
        </Border>
    </Border>

并且在资源中有一个datatemplate:

<DataTemplate x:Key="contentTemplate">
            <Grid>
                <Grid>
                    <ContentPresenter Content="{Binding ImagenSlideActual}" />
                </Grid>
                <Border BorderBrush="Black" BorderThickness="1" Background="#80000000" Height="80" VerticalAlignment="Bottom">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Button Style="{StaticResource BotonGrande}" Name="BotonImagenAtras" Click="BotonImagenAtras_Click">
                            <Image Style="{StaticResource ImagenGrande}" Source="/VisorSeproban;component/Imagenes/izquierda.png" />
                        </Button>
                        <Button Style="{StaticResource BotonGrande}" Name="BotonImagenesPlay" Click="BotonImagenesPlay_Click">
                            <Image Style="{StaticResource ImagenGrande}" Source="/VisorSeproban;component/Imagenes/play_on.png" />
                        </Button>
                        <Button Style="{StaticResource BotonGrande}" Name="BotonCaputarImagen" Click="BotonCaputarImagen_Click">
                            <Image Style="{StaticResource ImagenGrande}" Source="/VisorSeproban;component/Imagenes/captura_img_on.png" />
                        </Button>
                        <Button Style="{StaticResource BotonGrande}" Name="BotonImagenAdelante" Click="BotonImagenAdelante_Click">
                            <Image Style="{StaticResource ImagenGrande}" Source="/VisorSeproban;component/Imagenes/derecha.png" />
                        </Button>
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>

在我的c#代码中,我想在名为ContenidoVentanaEmergente的contentpreseter中应用此数据模板,并在datatemplate上的其他内容展示器中显示图像。

要应用我做的模板

RadTileViewItem item = sender as RadTileViewItem;
        ImagenSlideActual = ObtenerImagen(item.Uid);

        if (ImagenSlideActual != null)
        {
            ContenidoVentanaEmergente.Content = ImagenSlideActual;
            ContenidoVentanaEmergente.ContentTemplate = (DataTemplate)this.FindResource("contentTemplate");
            this.VentanaEmergente.Visibility = System.Windows.Visibility.Visible;
        }

模板有效但图像没有绑定,属性Image ImagenSlideActual是公开的。

如何在contentpresenter中绑定图像。?

1 个答案:

答案 0 :(得分:0)

我试过简化你的例子,它确实适合我。尝试检查样式是否在这里干扰,我确实删除了那些(BotonGrande和ImagenGrande)。我还添加了简单的空ViewModel作为内容。

<Window.Resources>
    <DataTemplate x:Key="contentTemplate">
        <Grid>
            <Grid>
                <ContentPresenter Content="{Binding ImagenSlideActual}" />
            </Grid>
            <Border BorderBrush="Black" BorderThickness="1" Background="#80000000" Height="80" VerticalAlignment="Bottom">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Button Name="BotonImagenesPlay" >
                        <Image Source="/Try;component/Imagenes/play_on.png" />
                    </Button>
                </StackPanel>
            </Border>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot">
    <ContentPresenter Name="ContenidoVentanaEmergente" />
</Grid> 

和背后的代码

ContenidoVentanaEmergente.Content = vm;
ContenidoVentanaEmergente.ContentTemplate = (DataTemplate)this.FindResource("contentTemplate");

如果没有任何作用,请确保您已将图像添加到项目中,并且您没有更改图像上的BuildAction - 我将其作为资源。

相关问题