我已经编写了这个代码,用于将模板应用到ComboBox。
这是App.xaml上的代码
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox" x:Name="selectedStyleTrigger">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="Width" Value="150"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2"
Focusable="False" IsChecked="{Binding Path=IsDropDownOpen,
Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,3,23,3"
Focusable="True" Background="Transparent" Foreground="White"
Visibility="Hidden" IsReadOnly="{TemplateBinding IsReadOnly}"
Width="250"/>
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"
Width="200">
<Grid Name="DropDown" SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" Background="#004460" BorderThickness="1" BorderBrush="#004460" />
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="True">
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="0,0,2,2"/>
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,-1,0,0"/>
</Trigger>
<Trigger Property="IsEditable" Value="True">
<Setter Property="IsTabStop" Value="False"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有这个类,它扩展了ComboBox:
public class ComboBoxExtends :ComboBox
{
public LOCAL_PE_AttivitaXParametri parametri { set; get; }
}
现在我以这种方式在Panel上插入ComboBox:
ComboBoxExtends comboBox = new ComboBoxExtends();
comboBox.Style = Resources["selectedStyleTrigger"] as Style;
grid.Children.Add(comboBox);
我将样式“comboBox.Style = Resources [”selectedStyleTrigger“]设置为Style;”但我没有看到这种风格。
我该如何解决?
Reguards
答案 0 :(得分:0)
您已为Style
创建了ComboBox
,并且正在尝试将其应用于不是ComboBoxExtends
的自定义ComboBox
课程。为了为您的自定义类声明Style
,您需要添加一个XML命名空间前缀,该前缀指向声明它的程序集......可能是这样的:
xmlns:Local="clr-namespace:YourAppName.OptionalFolderName"
然后您可以使用此前缀引用您的自定义类:
<Style TargetType="Local:ComboBoxExtends" x:Key="selectedStyleTrigger">
...
</Style>