【问题标题】:WPF Wrap panel not working for radio button with content template binding style?WPF Wrap 面板不适用于具有内容模板绑定样式的单选按钮?
【发布时间】:2014-10-24 06:42:05
【问题描述】:

我是 WPF 的新手。我在 WPF 应用程序的菜单中有单选按钮列表。我将单选按钮的样式更改为看起来像文本块作为子菜单。现在的问题是,当我尝试在单选按钮内包装图像和文本时,它没有显示出来。但是如果我删除了我在单选按钮上添加的样式,使其看起来和感觉像文本块,那么包装就可以正常工作,但是它会显示我不需要的单选按钮图标。

请参考以下代码:

<StackPanel.Resources>
<Style x:Key="RadioButtonMenuStyle" TargetType="RadioButton">
<Setter Property="Template">                      
<Setter.Value> 
<ControlTemplate TargetType="{x:Type RadioButton}"> 
<TextBlock Text="{TemplateBinding Content}"> 
<TextBlock.Style> 
<Style TargetType="{x:Type TextBlock}"> 
<Setter Property="Height" Value="36"/> 
<Setter Property="Foreground" Value="Black" /> 
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Center"/> 
<Setter Property="TextWrapping" Value="Wrap"/> 
</TextBlock.Style> 
</TextBlock> 
</ControlTemplate> 
</Setter.Value> 
</Setter> 
</Style> 
</StackPanel.Resources>

------------------------------------------------------------------------

<RadioButton Name="rbttest" GroupName="rbtgroup" Background="{x:Null}" Style="   

{StaticResource RadioButtonMenuStyle}"  IsChecked="True">

<WrapPanel>
<Image Source="/WpfApplication1;component/Images/Test.ico" Width="16" Height="16"    
Margin="0,0,5,0" />
<TextBlock Text="Test" Foreground="Green" />
</WrapPanel>                    
</RadioButton>

是模板绑定内容导致问题吗?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    在您的模板中,您将TextBlock.Text 绑定到RadioButton.Content,但您的RadioButton 内容实际上是一个UI 元素(WrapPanel)。

    去掉模板中的TextBlock,用ContentPresenter代替。您不需要在演示者上设置任何属性;它已经知道如何在模板化父级上定位内容。

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 2013-04-05
      • 2015-10-25
      • 1970-01-01
      • 2015-04-09
      • 2013-04-03
      • 2014-11-05
      • 2017-04-04
      • 1970-01-01
      相关资源
      最近更新 更多