ControlTemplate 中的 DataTrigger 未设置图像源 [英] DataTrigger in ControlTemplate not setting Image source

查看:24
本文介绍了ControlTemplate 中的 DataTrigger 未设置图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个 ControlTemplate,用于以我需要的格式显示视图模型.但是,我无法从控件模板的 DataTrigger 设置图像的源属性.请帮我解决这个问题.

I have a ControlTemplate that i defined for showing a View model in my required format. However, I am not able to set the Image's source property from the DataTrigger of the Control Template. Please help me with this.

<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 属性.但是,图像没有变化.

I am trying to set the Source property for imgStatus in the ControlTemplate triggers. But, there is no change in the Image.

推荐答案

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

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

It became clear that i should not use TemplatedParent. Setting the {RelativeSource Self} in the binding solved the problem.

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

这篇关于ControlTemplate 中的 DataTrigger 未设置图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆