#c# #wpf #binding #datatemplate #contentpresenter
#c# #wpf #привязка #datatemplate #contentpresenter
Вопрос:
у меня есть наложение wpf с contentpresenter:
<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 # я хочу применить этот шаблон данных в contentpreseter с именем ContenidoVentanaEmergente и отобразить изображение в другом представлении контента на 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:
Я попробовал упростить ваш пример, и у меня это сработало. Попробуйте проверить, не мешают ли здесь стили, я действительно удалил их (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");
Если у вас ничего не работает, убедитесь, что у вас есть изображения, добавленные в project, и вы не изменили BuildAction для изображений — у меня это как ресурс.