ComboBox模版样式绑定问题~

gaosam 2013-08-30 09:08:42
Item样式
<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;

选中项的值显示的是实体t_book
如何绑定DisplayMemberPath 和SelectedValuePath ?
...全文
202 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaosam 2013-09-03
  • 打赏
  • 举报
回复
<TextBlock Text="{Binding FName,Mode=TwoWay}" />
<TextBlock Text="{Binding ID,Mode=TwoWay}" />

绑个字段不是FName的这个样式就用不了了~
HFtime 2013-09-02
  • 打赏
  • 举报
回复
<DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding FName,Mode=TwoWay}" /> <TextBlock Text="{Binding ID,Mode=TwoWay}" /> </StackPanel> </DataTemplate> 代码里的 this.dropBookID.DisplayMemberPath = "FName"; 不要设
HFtime 2013-09-02
  • 打赏
  • 举报
回复
设置DataTemplate!
gaosam 2013-08-30
  • 打赏
  • 举报
回复
意思是我后台绑定值以后(功能正常,值绑定成功的),控件的样式改过模板造成无法显示绑定的值
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,不知道怎么写代码
灬浪子灬 2013-08-30
  • 打赏
  • 举报
回复
dropBookID.DisplayMemberPath = "FName"; SelectedValue="BookID" SelectedValuePath = "ID"; 看的不是很明白
gaosam 2013-08-30
  • 打赏
  • 举报
回复
DisplayMemberPath = "FName"后台已写,主要是样式里的模版改动后显示不出值来 ComboBox样式倒还好,用Text绑定就能显示了
<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" />
显示一片空白~~
灬浪子灬 2013-08-30
  • 打赏
  • 举报
回复
<ComboBox Name="dropBookID" Width="220" Grid.Row="2" SelectedValue="BookID}" DisplayMemberPath = "FName" Style="{StaticResource DefaultComboBoxStyle}" HorizontalAlignment="Left" />

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧