XAML:如何在样式中使用绑定转换器?

时间:2010-12-10 08:16:04

标签: binding

我在Sytle中使用了转换器,但似乎转换器从未被调用过......

代码是这样的:

<Style x:Key="DependencyToolTipStyle" TargetType="{x:Type ToolTip}">
    <Style.Resources>
        <local:ConvertTypeToTypeCN x:Key="Converter1"></local:ConvertTypeToTypeCN>
    </Style.Resources>
            <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border  Background="LightYellow" BorderThickness="1" BorderBrush="Black">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition  />
                            <RowDefinition  />
                        </Grid.RowDefinitions>
                        <TextBlock Margin="1" Text="type:" Grid.Column="0" Grid.Row="0" />
                        <TextBlock Margin="1" Grid.ColumnSpan="2"  Grid.Column="1" Grid.Row="0">
                            <TextBlock.Text>
                                **<Binding Path="LinkType" Converter="{StaticResource Converter1}"/>** 
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Margin="1" Text="Lag:" Grid.Column="0" Grid.Row="1" />
                        <TextBlock Margin="1"  Grid.Column="1" Grid.Row="1" Text="{Binding Lag}" />                          
                        <TextBlock Margin="1" Text="From:" Grid.Column="0" Grid.Row="2" />
                        <TextBlock Margin="1"  Grid.Column="1" Grid.Row="2" Text="{Binding Path=From.ID}" />
                        <TextBlock Margin="1"  Grid.Column="2" Grid.Row="2" Text="{Binding Path=From.Name}" />
                        <TextBlock Margin="1" Text="To:" Grid.Column="0" Grid.Row="3" />
                        <TextBlock Margin="1" Grid.Column="1" Grid.Row="3" Text="{Binding Path=To.ID}" />
                        <TextBlock Margin="1"  Grid.Column="2" Grid.Row="3" Text="{Binding Path=To.Name}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ToolTip x:Key="DependencyToolTip" Style="{StaticResource DependencyToolTipStyle}"/>
<Style  TargetType="controls:GanttDependency">
    <Setter Property="ToolTip" Value="{StaticResource DependencyToolTip}" />
</Style>

1 个答案:

答案 0 :(得分:0)

尝试将转换器定义为网格的资源。

<Style x:Key="DependencyToolTipStyle" TargetType="{x:Type ToolTip}">
            <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border  Background="LightYellow" BorderThickness="1" BorderBrush="Black">
                    <Grid>
                        **<Grid.Resources>
                            <local:ConvertTypeToTypeCN x:Key="Converter1"></local:ConvertTypeToTypeCN>
                        </Grid.Resources>**
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition  />
                            <RowDefinition  />
                        </Grid.RowDefinitions>
                        <TextBlock Margin="1" Text="type:" Grid.Column="0" Grid.Row="0" />
                        <TextBlock Margin="1" Grid.ColumnSpan="2"  Grid.Column="1" Grid.Row="0">
                            <TextBlock.Text>
                                **<Binding Path="LinkType" Converter="{StaticResource Converter1}"/>** 
                            </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Margin="1" Text="Lag:" Grid.Column="0" Grid.Row="1" />
                        <TextBlock Margin="1"  Grid.Column="1" Grid.Row="1" Text="{Binding Lag}" />                          
                        <TextBlock Margin="1" Text="From:" Grid.Column="0" Grid.Row="2" />
                        <TextBlock Margin="1"  Grid.Column="1" Grid.Row="2" Text="{Binding Path=From.ID}" />
                        <TextBlock Margin="1"  Grid.Column="2" Grid.Row="2" Text="{Binding Path=From.Name}" />
                        <TextBlock Margin="1" Text="To:" Grid.Column="0" Grid.Row="3" />
                        <TextBlock Margin="1" Grid.Column="1" Grid.Row="3" Text="{Binding Path=To.ID}" />
                        <TextBlock Margin="1"  Grid.Column="2" Grid.Row="3" Text="{Binding Path=To.Name}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ToolTip x:Key="DependencyToolTip" Style="{StaticResource DependencyToolTipStyle}"/>
<Style  TargetType="controls:GanttDependency">
    <Setter Property="ToolTip" Value="{StaticResource DependencyToolTip}" />
</Style>