我到达了组合框中的List,但显示名称不正确。如果我放下列表,则以不正确的方式显示项目。让我告诉你:
但是当我选择项目时,属性显示正确:
这是我的模特:
class RouteModel : NotificationObject
{
#region Fields
...
private string code;
...
#endregion
#region Properties
...
public string Code
{
get { return code; }
set
{
if (code != value)
{
code = value;
RaisePropertyChanged(() => Code);
}
}
}
...
这是ViewModel:
public PlanningViewModel()
{
_routesCollection = new List<RouteModel>();
foreach (RouteModel route in GetRoutes())
_routesCollection.Add(route);
}
private static TOPEntities _context;
private List<RouteModel> _routesCollection;
public List<RouteModel> RoutesCollection
{
get { return _routesCollection; }
set
{
if (_routesCollection != value)
{
_routesCollection = value;
RaisePropertyChanged(() => RoutesCollection);
}
}
}
public List<RouteModel> GetRoutes()
{
using (_context = new TOPEntities())
{
{
return _context.Routes.Select
(route => new RouteModel
{
Id_Route = route.Id_Route,
Code = route.Code,
MaxDepartTimeSB = route.MaxDepartTimeSB,
MaxDepartTimeDirect = route.MaxDepartTimeDirect,
Carrier_Id_Carrier = route.Carrier_Id_Carrier,
Address = route.Address,
City = route.City,
PostalCode = route.PostalCode,
Country = route.Country,
DestinationName = route.DestinationName,
Carrier = route.Carrier,
Loads = route.Loads
}).ToList();
}
}
}
这是视图:
<Window x:Class="TOP2.Views.PlanningWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:TOP2.Views"
Title="{DynamicResource planningTitle}" Height="768" Width="1080" WindowStartupLocation="CenterScreen"
Icon="Images/truck.ico" AllowsTransparency="True" Background="Transparent" ResizeMode="NoResize" WindowStyle="None">
<Canvas HorizontalAlignment="Left" Height="738" VerticalAlignment="Top" Width="1078">
<Button x:Name="addTruckBtn" Content="{DynamicResource addTruckBtn}" Canvas.Left="156" Canvas.Top="32" Width="93" />
<Button x:Name="addLoadBtn" Content="{DynamicResource addLoadBtn}" Canvas.Left="156" Canvas.Top="65" Width="93" />
<ComboBox x:Name="routesCmbx" Canvas.Left="10" Canvas.Top="32" Width="127" ItemsSource="{Binding RoutesCollection}" DisplayMemberPath="Code"/>
<Button x:Name="btnexport" Canvas.Left="1019" Canvas.Top="692" Width="46" RenderTransformOrigin="-0.4,0.364" Height="46">
<Image Source="Images/excel.png" Stretch="None" />
</Button>
<Button x:Name="btnprint" Canvas.Left="958" Canvas.Top="692" Width="46" RenderTransformOrigin="-0.4,0.364" Height="46">
<Image Source="Images/printer.png" Stretch="None" />
</Button>
</Canvas>
</Window>
DataContext是外部分配的。我不需要更新或编辑项目,只需选择一项。谁知道发生了什么?
更新
这是应用的样式,似乎错误在这里:
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}"/>
</ControlTemplate>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Foreground" Value="{StaticResource OutsideFontColor}"/>
<Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" />
</Style>
<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
<ControlTemplate.Resources>
<Storyboard x:Key="FocusedOn">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="FocusedOff">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid>
<ToggleButton Grid.Column="2" Template="{DynamicResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
<ContentPresenter HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False"/>
<TextBox Visibility="Hidden" Template="{DynamicResource ComboBoxTextBox}" HorizontalAlignment="Left" Margin="3,3,23,3" x:Name="PART_EditableTextBox" Style="{x:Null}" VerticalAlignment="Center" Focusable="True" Background="Transparent" IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Rectangle x:Name="DisabledVisualElement" Fill="#A5FFFFFF" RadiusX="4" RadiusY="4" IsHitTestVisible="false" Visibility="Collapsed" />
<Rectangle x:Name="FocusVisualElement" Margin="-1" Stroke="{StaticResource selectedStroke}" StrokeThickness="1" RadiusX="4" RadiusY="4" IsHitTestVisible="false" Opacity="0"/>
<Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
<Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
<Border x:Name="DropDownBorder" Background="#FFFFFFFF" BorderBrush="{StaticResource TextBoxNorm}" BorderThickness="1" CornerRadius="1,1,3,3">
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
</ScrollViewer>
</Border>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource FocusedOff}" x:Name="FocusedOff_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource FocusedOn}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter Property="MinHeight" Value="95" TargetName="DropDownBorder"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
<Setter Property="Visibility" TargetName="DisabledVisualElement" Value="Visible"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="AllowsTransparency" SourceName="Popup" Value="true">
<Setter Property="CornerRadius" Value="4" TargetName="DropDownBorder"/>
<Setter Property="Margin" Value="0,2,0,0" TargetName="DropDownBorder"/>
</Trigger>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Visibility" Value="Visible" TargetName="PART_EditableTextBox"/>
<Setter Property="Visibility" Value="Hidden" TargetName="ContentSite"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style d:IsControlPart="True" TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="3" />
<Setter Property="Foreground" Value="{StaticResource OutsideFontColor}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<ControlTemplate.Resources>
<Storyboard x:Key="HoverOn">
<DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientOver" Storyboard.TargetProperty="Opacity" To="0.73"/>
</Storyboard>
<Storyboard x:Key="HoverOff">
<DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientOver" Storyboard.TargetProperty="Opacity" To="0"/>
</Storyboard>
<Storyboard x:Key="SelectedOn">
<DoubleAnimation Duration="00:00:00.1000000" Storyboard.TargetName="BackgroundGradientSelected" Storyboard.TargetProperty="Opacity" To="0.84"/>
</Storyboard>
<Storyboard x:Key="SelectedOff">
<DoubleAnimation Duration="00:00:00.4000000" Storyboard.TargetName="BackgroundGradientSelected" Storyboard.TargetProperty="Opacity" To="0"/>
</Storyboard>
</ControlTemplate.Resources>
<Grid SnapsToDevicePixels="true">
<Rectangle x:Name="BackgroundGradientOver" Fill="{StaticResource hoverGradient}" Stroke="{StaticResource hoverStroke}" RadiusX="2" RadiusY="2" Opacity="0"/>
<Rectangle x:Name="BackgroundGradientSelected" Fill="{StaticResource BtnOverFill}" Stroke="{StaticResource selectedStroke}" RadiusX="2" RadiusY="2" Opacity="0"/>
<Rectangle x:Name="BackgroundHighlight" Margin="1" Stroke="#A0FFFFFF" RadiusX="1" RadiusY="1"/>
<ContentPresenter
x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource HoverOn}" x:Name="HoverOn_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsHighlighted" Value="true"/>
<Trigger Property="Selector.IsSelected" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource SelectedOff}" x:Name="SelectedOff_BeginStoryboard1"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource SelectedOn}" x:Name="SelectedOn_BeginStoryboard1"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:2)
如果您想使用“WhistlerBlue”风格,您应该定义ItemTemplate
:
...
<ComboBox x:Name="routesCmbx" Canvas.Left="10" Canvas.Top="32" Width="127" ItemsSource="{Binding RoutesCollection}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Code}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
...