从使用自定义控件的用户控件发送参数

时间:2015-09-11 03:08:43

标签: c# .net wpf xaml custom-controls

我想从使用自定义控件的用户控件传递参数,并在我的自定义控件的cs上使用它。例如,如果我在UserControl上有自定义控件

在UserControl中(例如ThisViewName.XAML):

<ctrl:PinWindowControl Tag="ThisViewName" Grid.Row="0"/>

其中只包含一个按钮

Generic.xaml:

<Style TargetType="{x:Type local:PinWindowControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:PinWindowControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Button Width="100" Height="100"></Button>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如何将标签一直提供给我的PinWindowControl.cs文件?

1 个答案:

答案 0 :(得分:1)

假设您在DependencyProperty中创建了PinWindowControl

您可以使用Tag访问this.Tag。您的PinWindowControlpartial class相关联XAML

public class PinWindowControl.cs : FrameworkElement
{
  public PinWindowControl()
  {
     Debug.WriteLine(this.Tag);
  }
}