ControlTemplate中的DataTrigger未设置图像源

时间:2013-09-28 15:53:13

标签: c# wpf xaml

我有一个ControlTemplate,我定义为以我所需的格式显示View模型。但是,我无法从控件模板的DataTrigger设置Image的source属性。请帮帮我。

<ControlTemplate x:Key="GeneralControlTemplate"
                     TargetType="{x:Type local:ComponentStatisticsControl}">
        <Border CornerRadius="3"
                BorderThickness="2"
                BorderBrush="SkyBlue"
                Margin="8,0,0,0">
            <Border.Background>
                <SolidColorBrush x:Name="ControlBackground" Color="Transparent"/>
            </Border.Background>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Label Foreground="Blue"
                       Grid.Row="0"
                       FontSize="14"
                       FontWeight="SemiBold"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Content="{Binding Path=Title, RelativeSource={RelativeSource TemplatedParent}}"/>

                <Grid Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <GroupBox Header="Component Info"
                              Grid.Row="0"
                              Foreground="Maroon"
                              FontWeight="SemiBold">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <Label Content="IP Address"
                                   Grid.Column="0"
                                   Grid.Row="0"/>
                            <Label Content="{Binding Path=AdditionalContent.IPAddress, RelativeSource={RelativeSource TemplatedParent}}"
                                   Grid.Row="0"
                                   HorizontalAlignment="Right"
                                   Grid.Column="1"/>

                            <Label Content="PortNo"
                                   Grid.Column="0"
                                   Grid.Row="1"/>
                            <Label Content="{Binding Path=AdditionalContent.PortNo, RelativeSource={RelativeSource TemplatedParent}}"
                                   Grid.Row="1"
                                   HorizontalAlignment="Right"
                                   Grid.Column="1"/>

                            <Label Content="Status"
                                   Grid.Column="0"
                                   Grid.Row="2"/>
                            <Image Name="imgStatus"
                                   Height="24"
                                   Width="24"
                                   Stretch="Fill"
                                   HorizontalAlignment="Right"
                                   Grid.Column="1"
                                   Grid.Row="2">
                            </Image>
                        </Grid>
                    </GroupBox>
                    <Expander Header="Queue Statistics"
                              Grid.Row="1"
                              ExpandDirection="Down"
                              IsExpanded="True"
                              Foreground="DarkOrange"
                              FontWeight="SemiBold">
                        <ItemsControl ItemsSource="{Binding Path=AdditionalContent.QueueStatistics, RelativeSource={RelativeSource TemplatedParent}}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Grid.IsSharedSizeScope="True"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate DataType="{x:Type lappmodel:clsQueueStatistics}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition SharedSizeGroup="ColumnOne"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Label Content="{Binding QName}"/>
                                        <Label Content="{Binding Count}"
                                               Margin="8,0,0,0"
                                               HorizontalAlignment="Right"
                                               Grid.Column="1"/>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Expander>
                    <GroupBox Header="Connection Statistics"
                              Foreground="DarkOrange"
                              Grid.Row="2"
                              FontWeight="SemiBold">
                        <ItemsControl ItemsSource="{Binding Path=AdditionalContent.ConnectionInformation, RelativeSource={RelativeSource TemplatedParent}}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Grid.IsSharedSizeScope="True" Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate DataType="{x:Type lappmodel:clsComponentConnectionInfo}">
                                    <Border CornerRadius="3"
                                            BorderThickness="1"
                                            BorderBrush="Gray"
                                            x:Name="borderConnectionInfo">
                                        <Border.Background>
                                            <SolidColorBrush Color="Transparent"/>
                                        </Border.Background>
                                        <Grid>
                                            <Grid.Background>
                                                <SolidColorBrush x:Name="gridconnectioninfo" 
                                                                 Color="Transparent"/>
                                            </Grid.Background>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition SharedSizeGroup="ColumnOne"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <Label Content="Component"
                                                   Grid.Row="0"/>
                                            <Label Content="{Binding Identifier}"
                                                       Grid.Column="1"
                                                       Grid.Row="0"
                                                       HorizontalAlignment="Right"/>
                                            <Label Content="Component Id"
                                                   Grid.Row="1"/>
                                            <Label Content="{Binding ComponentId}"
                                                   Grid.Column="1"
                                                   Grid.Row="1"
                                                   Foreground="Blue"
                                                   HorizontalAlignment="Right"/>
                                            <Label Content="BytesSent"
                                                   Grid.Row="2"/>
                                            <Label Content="{Binding BytesSent}"
                                                   Grid.Row="2"
                                                   Grid.Column="1"
                                                   HorizontalAlignment="Right"/>

                                            <Label Content="BytesReceived"
                                                   Grid.Row="3"/>
                                            <Label Content="{Binding BytesReceived}"
                                                   Grid.Row="3"
                                                   Grid.Column="1"
                                                   HorizontalAlignment="Right"/>
                                            <Label Content="Connected"
                                                   Grid.Row="4"/>
                                            <Image x:Name="imgConnectedStatus"
                                                   Grid.Row="4"
                                                   Grid.Column="1"
                                                   HorizontalAlignment="Right"
                                                   Height="24"
                                                   Width="24">
                                            </Image>
                                        </Grid>
                                    </Border>
                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding IsConnected}" Value="True">
                                            <Setter Property="Source"
                                                    TargetName="imgConnectedStatus"
                                                    Value="/Resources/GreenDot.png"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding IsConnected}" Value="False">
                                            <Setter Property="Source"
                                                    TargetName="imgConnectedStatus"
                                                    Value="/Resources/red-dot2.png"/>
                                            <DataTrigger.EnterActions>
                                                <BeginStoryboard>
                                                    <Storyboard Duration="0:0:0.200"
                                                                AutoReverse="True"
                                                                RepeatBehavior="Forever">
                                                        <ColorAnimation From="Red"
                                                                        To="DarkRed"
                                                                        Storyboard.TargetName="borderConnectionInfo"
                                                                        Storyboard.TargetProperty="Background.Color"/>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </DataTrigger.EnterActions>
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </GroupBox>
                </Grid>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding AdditionalContent.IsComponentShutdown, RelativeSource={RelativeSource TemplatedParent}}"
                         Value="False">
                <Setter TargetName="imgStatus"
                        Property="Source"
                        Value="/Resources/GreenDot.png"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

我正在尝试在ControlTemplate触发器中设置imgStatus的Source属性。 但是,图像没有变化。

2 个答案:

答案 0 :(得分:0)

精细,奇怪的控制设计......

    <DataTrigger Binding="{Binding Path=DataContext.IsConnected, 
         RelativeSource={RelativeSource AncestorType=ItemsControl}}" Value="True">
            <Setter Property="Source"
                    TargetName="imgConnectedStatus"
                    Value="/Resources/GreenDot.png"/>
     </DataTrigger>

因为您正在使用项目预先设置的DataTrigger,所以DataTemplates DataContext是ItemsSource中的一个对象,RelativeSource是包含控件的对象,它的路径是DataContext.Property

答案 1 :(得分:0)

通过查看以下链接 http://social.msdn.microsoft.com/Forums/vstudio/en-US/a3bf91a8-e618-41c6-a1ad-be9e19581fd6/datatrigger-inside-controltemplate-issue

很明显我不应该使用TemplatedParent。在绑定中设置{RelativeSource Self}解决了问题。

        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding AdditionalContent.IsComponentShutdown, RelativeSource={RelativeSource Self}}"
                         Value="False">
                <Setter TargetName="imgStatus"
                        Property="Source"
                        Value="/Resources/GreenDot.png"/>
            </DataTrigger>
        </ControlTemplate.Triggers>