添加包装到某些控件

时间:2018-04-06 08:17:42

标签: c# asp.net xaml silverlight

我需要在一个窗口上添加包装到我的单选按钮控件。当单选按钮添加到不需要换行的不同页面时,我试图弄清楚如何从我的控件中有条件地删除或更改我的包装面板的值。

这是添加包装的编码。

XAML

<Style TargetType="auc:APRadioButtonListBox">
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="IsTabStop" Value="True" />
        <Setter Property="FontSize" Value="{StaticResource FontSize}" />
        <Setter Property="Margin" Value="-2,0,0,0" />
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel Background="Transparent" ItemWidth="Auto" x:Name="RadioListWrapPanel" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">

C#

  public virtual ItemsPanelTemplate ItemsPanel { get; set; }

任何人都可以建议我如何在没有包装的情况下将单选按钮添加到另一页面。

2 个答案:

答案 0 :(得分:1)

由于您在应用程序级别应用了样式,因此它将隐式应用于与其目标类型匹配的所有控件。你有两个选项来获取一个没有包装的页面...

  • 使用省略包装的样式覆盖所需页面上的样式
  • 为您提供一个密钥的默认样式,并明确地将其应用于需要的地方,而其他人则不会进行包装。

如果更多的页面不需要包装,那么后者是有意义的。请考虑以下

<Style x:Key="WrappedRadioButtons" TargetType="auc:APRadioButtonListBox">
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="IsTabStop" Value="True" />
        <Setter Property="FontSize" Value="{StaticResource FontSize}" />
        <Setter Property="Margin" Value="-2,0,0,0" />
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel Background="Transparent" ItemWidth="Auto" x:Name="RadioListWrapPanel" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
</Style>

现在,您需要应用此样式的页面中,您可以执行以下操作:

<uac:APRadioButtonListBox Style="{StaticResource WrappedRadioButtons}" />

任何其他未明确引用此样式的内容都将获得默认设置。或者,您可以在不包装的情况下定义其他样式,并为其指定一个可以应用于不需要包装的页面的键。

答案 1 :(得分:1)

在不需要WrapPanel的页面上创建从base和override样式setter派生的新样式

<Style TargetType="auc:APRadioButtonListBox" 
       BasedOn="{StaticResource {x:Type auc:APRadioButtonListBox}}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

或者只是为auc执行:APRadioButtonListBox元素:

<auc:APRadioButtonListBox>
    <auc:APRadioButtonListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </auc:APRadioButtonListBox.ItemsPanel>
</auc:APRadioButtonListBox>

或者在资源字典中定义一个没有设置ItemsPanel的命名样式。默认样式将扩展它。并且在一个不需要换行的页面上使用命名样式:

<Style TargetType="auc:APRadioButtonListBox" x:Key="APRadioButtonListBoxStyle">
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
    <Setter Property="IsTabStop" Value="True" />
    <Setter Property="FontSize" Value="{StaticResource FontSize}" />
    <Setter Property="Margin" Value="-2,0,0,0" />
</Style>

<Style TargetType="auc:APRadioButtonListBox" BasedOn="{StaticResource APRadioButtonListBoxStyle}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel Background="Transparent" ItemWidth="Auto" x:Name="RadioListWrapPanel" Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

<auc:APRadioButtonListBox Style="{StaticResource APRadioButtonListBoxStyle}"/>
相关问题