获取模板视图对外部参数可见

时间:2015-05-14 15:26:55

标签: c# xaml silverlight templates

在XAML世界中,我有一个问题,即要使用此模板/样式公开要查看的模板/样式的数据。这是一个例子。 第一个样式模板:

<Style TargetType="myStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="myStyle">
                    <telerik:RadGridView Name="SomeName"
                             ItemsSource="{TemplateBinding SomeSource}"

                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding SomeBinding}" Header="SomeHeader"/>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding SomeBinding2}" Header="SomeHeader2" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding SomeBinding3}" Header="SomeHeader3" IsReadOnly="True">
                                <telerik:GridViewDataColumn.CellTemplate>
                                    <DataTemplate>
                                        <Controls DataContext="{Binding SomeBinding4}" />
                                    </DataTemplate>
                                </telerik:GridViewDataColumn.CellTemplate>
                            </telerik:GridViewDataColumn>
                        </telerik:RadGridView.Columns>                           
                    </telerik:RadGridView>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

将使用它的xaml文件:

            <myStyle
            Grid.Row="0"
            SomeSource="{Binding SomeExternalSource}">

            <!-- I want here behaviour that will interact with myStyle Columns for example exporting this data to excel-->

            </myStyle>

1 个答案:

答案 0 :(得分:0)

当您创建自定义ControlsViews并且希望它们具有某些(技术)界面以便您可以使用xaml标记或代码与您的自定义类型进行交互时,我建议您添加公共DependencyProperties

想象一下,你有一个控件 MySpecialGrid

public class MySpecialGrid : Control
{
    ctor() {
        DefaultStyleKey = typeof(MySpecialGrid);
        Columns = new RadGridViewColumnsConfiguration();
    }

    public RadGridViewColumnsConfiguration Columns
    {
        get { return (RadGridViewColumnsConfiguration) GetValue( ColumnsProperty ); }
        set { SetValue( ColumnsProperty, value ); }
    }

    public static readonly DependencyProperty ColumnsProperty =
        DependencyProperty.Register( "Columns", typeof( RadGridViewColumnsConfiguration),
        typeof( MySpecialGrid), null );
}

MySpecialGrid

的默认样式
<Style TargetType="MySpecialGrid">
  <Setter Property="Columns">
    <Setter.Value>
      <RadGridViewColumnsConfiguration>
        <GridViewDataColumn DataMemberBinding="{Binding SomeBinding}" Header="SomeHeader"/>
        <GridViewDataColumn DataMemberBinding="{Binding SomeBinding2}" Header="SomeHeader2"/>
        <GridViewDataColumn DataMemberBinding="{Binding SomeBinding3}" Header="SomeHeader3">
          <GridViewDataColumn.CellTemplate>
            <DataTemplate>
              <... />
            </DataTemplate>
          </GridViewDataColumn.CellTemplate>
        </GridViewDataColumn>
      </RadGridViewColumnsConfiguration>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="MySpecialGrid">
        <RadGridView Columns="{TemplateBinding Columns}"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

您现在可以从控件外部访问它,如下所示:

<MySpecialGrid x:Name="FeeblefezerDataGrid" ... />
<MyExcelExportButton Content="Export To Excel"
    ColumnsConfiguration="{Binding ElementName=FeeblefezerDataGrid, Path=Columns}"/>