8,757
社区成员
发帖
与我相关
我的任务
分享<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Height" Value="28" />
<Setter Property="Width" Value="{Binding Width}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="140*" />
</Grid.ColumnDefinitions>
<Border x:Name="itembg" Grid.ColumnSpan="2" Width="{TemplateBinding Width}" Height="30" Background="White"
BorderThickness="2,1,2,1" BorderBrush="#bcbcbc" VerticalAlignment="Bottom" ></Border>
<Border Width="1" Height="30" Background="#bcbcbc" CornerRadius="15" Margin="0,1,0,1"
BorderThickness="2" BorderBrush="#bcbcbc" VerticalAlignment="Bottom" HorizontalAlignment="Right"></Border>
<TextBlock Text="{Binding ID}" HorizontalAlignment="Right" Margin="0,0,3,0" />
<TextBlock Text="{Binding FName}" Grid.Column="1" HorizontalAlignment="Left" Margin="3,0,0,0" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="itembg" Property="Background" Value="#c1c1c1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="DefaultComboBoxStyle" TargetType="ComboBox">
<Setter Property="Background" Value="White"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="BorderBrush" Value="#bcbcbc" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid x:Name="MainGrid" >
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border Background="Transparent" BorderThickness="0,0,0,2" BorderBrush="#bcbcbc" />
<ScrollViewer SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
<ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
<ContentPresenter Margin="10,0,0,0"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsHitTestVisible="false"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style><ComboBox Name="dropBookID" Width="220" Grid.Row="2" SelectedValue="{Binding BookID, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DefaultComboBoxStyle}" HorizontalAlignment="Left" />IList<t_book> book = (List<t_book>)DTList.DateTabletoList<t_book>(new t_book().GetList("").Tables[0]);
dropBookID.ItemsSource = book;
this.dropBookID.DisplayMemberPath = "FName";
this.dropBookID.SelectedValuePath = "ID";
this.dropBookID.SelectedIndex = 0;IList<t_book> book = (List<t_book>)DTList.DateTabletoList<t_book>(new t_book().GetList("").Tables[0]); dropBookID.ItemsSource = book; this.dropBookID.DisplayMemberPath = "FName"; this.dropBookID.SelectedValuePath = "ID"; this.dropBookID.SelectedIndex = 0;
这是后台绑定,值是绑定成功的,再取值和操作都没有问题,但是下拉列表控件的样式改过了~
没办法动态显示绑定的值~
比如绑定this.dropBookID.DisplayMemberPath = "FName";
的时候 <TextBlock Text="{Binding FName}" 模版代码这样写能显示FName的值,但样式代码是通用的,绑定的值并不总是FName,所以这里的绑定想动态的绑定成DisplayMemberPath,不知道怎么写代码
<ContentPresenter Margin="10,0,0,0"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsHitTestVisible="false"
Content="{TemplateBinding Text}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"/>
但ComboBoxItem里想绑定后台绑定的DisplayMemberPath 值就显示不出来
<TextBlock Text="{TemplateBinding ComboBox.SelectedValue}" HorizontalAlignment="Right" Margin="0,0,3,0" />
显示一片空白~~