【问题标题】:WPF - Binding data templateWPF - 绑定数据模板
【发布时间】:2021-03-15 12:06:03
【问题描述】:

我正在尝试在 Telerik RadDiagram ShapeStyle 上绑定 <DataTemplate>

<telerik:RadDiagram x:Name="Diagram"
                            ShapeStyle="{StaticResource NodeStyle}"
                            GraphSource="{Binding GraphSource}"/>

这种风格看起来像这样:

    <Grid.Resources>
        <Style x:Key="NodeStyle" TargetType="telerik:RadDiagramShape">
            <Setter Property="Position" Value="{Binding Position, Mode=TwoWay}" />
            <Setter Property="Content" Value="{Binding}" />
            <Setter Property="Geometry" Value="{telerik:CommonShape ShapeType=RectangleShape}" />
            <Setter Property="ContentTemplate" >
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource StyleConverter}">
                        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
                        <Binding Path=""/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
...
</Grid.Resources>

样式转换器:

class StyleConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        FrameworkElement targetElement = values[0] as FrameworkElement;

        if (values[0] is BilledMsisdnViewModel)
            return targetElement.FindResource("BilledMsisdnControl");
        else if(values[0] is BilledImeiViewModel)
            return targetElement.FindResource("ImeiControl");

        return null;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这些资源(BilledMsisdnControl 和 ImeiControl)在 &lt;UserControl.Resources&gt; 中定义:

<UserControl.Resources>
    <ResourceDictionary>
        <local:BilledMsisdnControl x:Key="BilledMsisdnControl"/>
        <local:ImeiControl x:Key="ImeiControl"/>
        <local:StyleConverter x:Key="StyleConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

它们是由我的用户控件创建的。

所以我这里的目标是根据绑定视图模型类型选择用户控件(这部分工作),然后将其用作ContentTemplate(这不起作用)。我只在图表节点中获得 ImeiControl 或 BilledMsisdnControl 的对象命名空间和名称。

我该如何解决这个问题?

【问题讨论】:

    标签: wpf data-binding telerik


    【解决方案1】:

    根据documentation,您应该使用样式选择器

    public class NodeStyleSelector : StyleSelector 
    { 
        public Style BilledMsisdnStyle { get; set; } 
        public Style BilledImeiStyle { get; set; } 
     
        public override Style SelectStyle(object item, DependencyObject container) 
        { 
            if (item is BilledMsisdnViewModel) 
                return BilledMsisdnStyle; 
            else if (item is BilledImeiViewModel) 
                return BilledImeiStyle; 
            else return base.SelectStyle(item, container); 
        } 
    }
    

    使用ContentTemplate 定义样式。在 ContentTemplate 中添加您的用户控件。

    <Style x:Key="BilledMsisdnStyle" TargetType="telerik:RadDiagramShape">
        <Setter Property="Position" Value="{Binding Position, Mode=TwoWay}" />
        <Setter Property="Content" Value="{Binding}" />
        <Setter Property="Geometry" Value="{telerik:CommonShape ShapeType=RectangleShape}" />
        <Setter Property="ContentTemplate" >
            <Setter.Value> 
                <DataTemplate> 
                    <local:BilledMsisdnControl/> 
                </DataTemplate> 
            </Setter.Value> 
        </Setter>
    </Style>
    
    
    <Style x:Key="BilledImeiStyle" TargetType="telerik:RadDiagramShape">
        <Setter Property="Position" Value="{Binding Position, Mode=TwoWay}" />
        <Setter Property="Content" Value="{Binding}" />
        <Setter Property="Geometry" Value="{telerik:CommonShape ShapeType=RectangleShape}" />
        <Setter Property="ContentTemplate" >
            <Setter.Value> 
                <DataTemplate> 
                    <local:ImeiControl/> 
                </DataTemplate> 
            </Setter.Value> 
        </Setter>
    </Style>
    

    添加形状样式选择器

    <styleselectors:NodeStyleSelector x:Key="CustomShapeStyleSelector" 
                    BilledMsisdnStyle="{StaticResource BilledMsisdnStyle}" 
                    BilledImeiStyle="{StaticResource BilledImeiStyle}" /> 
    

    ShapeStyleSelector 用作您的RadDiagram

    <telerik:RadDiagram x:Name="Diagram"
                            ShapeStyleSelector="{StaticResource CustomShapeStyleSelecto}"
                            GraphSource="{Binding GraphSource}"/>
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2014-08-23
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      相关资源
      最近更新 更多