0

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

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

4

2 回答 2

1

通过查看以下链接 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>
于 2013-09-28T20:38:12.117 回答
0

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

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

由于您使用的是项目 prepective 中的 DataTrigger ,因此 DataTemplates DataContext 是您的 ItemsSource 的对象,RelativeSource 到包含控件, Path 到它的 DataContext.Property

于 2013-09-28T19:31:41.563 回答