【问题标题】:Add wrapping to certain controls为某些控件添加包装
【发布时间】:2018-09-16 05:18:40
【问题描述】:

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

这是添加包装的编码。

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; }

谁能建议我如何在不换行的情况下将单选按钮添加到另一个页面。

【问题讨论】:

  • 这种风格是在什么级别定义的?应用程序资源字典,还是特定的页面/容器控件?
  • 应用资源字典@Fabulous

标签: c# asp.net xaml silverlight


【解决方案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}" />

任何其他未明确引用此样式的都将获得默认设置。或者,您可以定义其他样式而不进行换行,并为其提供一个密钥,您可以将其应用于不需要换行的页面。

【讨论】:

    【解决方案2】:

    在不需要 WrapPanel 的页面上创建从 base 派生的新样式并覆盖样式设置器

    <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}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2015-07-10
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多