C# WPF将静态数据模板重写为动态版本
我想请某人解决我的动态重写数据模板问题 我已经准备好了数据模板C# WPF将静态数据模板重写为动态版本,c#,wpf,C#,Wpf,我想请某人解决我的动态重写数据模板问题 我已经准备好了数据模板 <Window.Resources> <DataTemplate x:Key="pictureTemplate"> <DataTemplate.Resources> <Style TargetType="Image"> <Setter Property="Widt
<Window.Resources>
<DataTemplate x:Key="pictureTemplate">
<DataTemplate.Resources>
<Style TargetType="Image">
<Setter Property="Width" Value="180" />
<Setter Property="Height" Value="120" />
<Setter Property="Margin" Value="10" />
</Style>
</DataTemplate.Resources>
<Image Source="{Binding Path=Location}" />
</DataTemplate>
</Window.Resources>
但是这个代码不起作用。我不知道是什么问题,但我已经尽了一切可能。
有人能帮我解决这个问题吗?
谢谢。不要用代码编写数据模板,而是在参考资料部分按如下所示进行
<Window.Resources>
<DataTemplate x:Key="Temp1">
<TextBlock Text="1"/>
</DataTemplate>
<DataTemplate x:Key="Temp2">
<TextBlock Text="2"/>
</DataTemplate>
</Window.Resources>
<ItemsControl Name="itemscontrol2" ItemTemplate="{StaticResource Temp1}" Foreground="White" ItemsSource="{Binding Lista}"/>
为什么不使用这样的工具来动态调整大小
<Grid Background="CadetBlue">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0.1*"/>
</Grid.RowDefinitions>
<ItemsControl Name="control" ItemsSource="{Binding Lista}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="2.jpg"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="Change" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>
为什么不选择绑定动态属性呢。在代码隐藏中编写模板可能需要使用工厂类。是的,这是可能的。但我需要动态更改(基于窗口大小)图像的宽度和高度。从您的示例中,我不理解是否可能更改此属性。您可以使数据模板依赖于widt/height。准确地解释你想要获得什么。我是WPF新手,我需要根据WPF窗口的实际大小更改Items控件中图像的大小。我认为,正如我前面所描述的那样,只需创建新的datatemplate,但这段代码不起作用,我不理解您的示例:-)当您的窗口被放大时,datatemplate中的图像也应该这样做吗?
<Window.Resources>
<DataTemplate x:Key="Temp1">
<TextBlock Text="1"/>
</DataTemplate>
<DataTemplate x:Key="Temp2">
<TextBlock Text="2"/>
</DataTemplate>
</Window.Resources>
<ItemsControl Name="itemscontrol2" ItemTemplate="{StaticResource Temp1}" Foreground="White" ItemsSource="{Binding Lista}"/>
itemscontrol2.ItemTemplate = (DataTemplate)Resources["Temp2"];
<Grid Background="CadetBlue">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0.1*"/>
</Grid.RowDefinitions>
<ItemsControl Name="control" ItemsSource="{Binding Lista}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="2.jpg"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="Change" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
</Grid>