简体   繁体   English

内容控件WPF中的图像DataTemplate

[英]image DataTemplate inside a contentcontrol WPF

i have a DataTemplate 我有一个DataTemplate

<DataTemplate x:Key="image">
  <Image x:Name="TheImage" />
     <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=OverallResult}">
                <DataTrigger.Value>
                    <local:resultType>Success</local:resultType>
                </DataTrigger.Value>
                <Setter TargetName="TheImage" Property="Source" Value="bin/Debug/Input/successx.jpg" />
            </DataTrigger>
 </DataTemplate>

with some trigger setters That Works fine in a GridView 一些触发器设置器,在GridView中可以正常工作

  <ListView Margin="292,54,0,50" Name="listViewCaseSequence" MinHeight="215" Width="203" Button.Click="OnClick" ItemsSource="{Binding TestCaseSequenceList}" HorizontalAlignment="Left">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Result" CellTemplate="{StaticResource image}" Width="40"/>
      ...

now i would like to use it in some kind of StackPanel. 现在我想在某种StackPanel中使用它。 I already found out that i could use a ContentControle 我已经发现我可以使用ContentControle

<StackPanel Orientation="Horizontal">
   <!-- doenst work --> <ContentControl ContentTemplate="{StaticResource image}" Content="{Binding OverallResult}" />
   <!-- works --> <TextBlock Text="{Binding OverallResult}" />
</StackPanel> 

The TextBlock works fine. TextBlock工作正常。 But im missing something at the ContentControle thats doesnt let it render the image? 但是我在ContentControle上缺少一些东西,那是不是让它渲染了图像?

A pointer to the right reading source would be fine too :) Thanks in advance. 指向正确阅读源的指针也可以:)预先感谢。

EDIT: 编辑:

 ...
 <DataTrigger Binding="{Binding}">
 ...
 <ContentControl ContentTemplate="{StaticResource image}" Content="{Binding OverallResult}"/>
 ...

Output Says: System.Windows.Data Error: 40 : BindingExpression path error: 'OverallResult' property not found on 'object' ''resultType' (HashCode=0)'. 输出说:System.Windows.Data错误:40:BindingExpression路径错误:在'object'''resultType'(HashCode = 0)'上找不到'OverallResult'属性。 BindingExpression:Path=OverallResult; BindingExpression:路径= OverallResult; DataItem='resultType' (HashCode=0); DataItem ='resultType'(HashCode = 0); target element is 'ContentPresenter' (Name=''); 目标元素是'ContentPresenter'(Name =''); target property is 'NoTarget' (type 'Object') 目标属性为“ NoTarget”(类型“ Object”)

But why does he find the >OverallResult< on the Textblock thats works ? 但是,为什么他在有效的Textblock上找到> OverallResult <?

EDIT2: Still not working EDIT2:仍然无法正常工作

 ...
 <DataTrigger Binding="{Binding}">
 ...
 <ContentControl ContentTemplate="{StaticResource image}"/>
 ...

Edit3: Working: Edit3:工作:

<DataTrigger Binding="{Binding Path=OverallResult}">
<ContentControl ContentTemplate="{StaticResource image}" Content="{Binding}"/>

The DataContext in the DataTemplate here is in fact the Content you set for the ContentControl . 实际上,此处的DataTemplateDataContext是您为ContentControl设置的Content Because it's already set to {Binding OverallResult} , so the Binding inside DataTemplate should be just {Binding} like this: 因为它已经设置为{Binding OverallResult} ,所以DataTemplate内部的Binding应该只是{Binding}如下所示:

<DataTemplate x:Key="image">
   <Image x:Name="TheImage" />
   <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}">
            <DataTrigger.Value>
                <local:resultType>Success</local:resultType>
            </DataTrigger.Value>
            <Setter TargetName="TheImage" Property="Source" 
                    Value="bin/Debug/Input/successx.jpg" />
        </DataTrigger>
   </DataTemplate.Triggers>
 </DataTemplate>

The above template should of course be used only for the StackPanel. 当然,以上模板仅应用于StackPanel。 For the ListView, just use the old DataTemplate. 对于ListView,只需使用旧的DataTemplate。

However I think the Content you set for the ContentControl in this case can be just {Binding} , then the DataContext in both cases (for ListView and ContentControl) should be the same and we can use just one DataTemplate: 但是我认为您在这种情况下为ContentControl设置的Content可以只是{Binding} ,那么在两种情况下(对于ListView和ContentControl),DataContext应该相同,并且我们只能使用一个DataTemplate:

<ContentControl ContentTemplate="{StaticResource image}" Content="{Binding}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM