首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

ComboBox模版样式绑定有关问题

2013-09-28 
ComboBox模版样式绑定问题~Item样式Style x:Key{x:Type ComboBoxItem} TargetType{x:Type ComboBoxI

ComboBox模版样式绑定问题~
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 ?
[解决办法]
dropBookID.DisplayMemberPath = "FName"; 
SelectedValue="BookID" 
SelectedValuePath = "ID"; 
看的不是很明白
[解决办法]
<DataTemplate>
<StackPanel  Orientation="Horizontal">
<TextBlock Text="{Binding FName,Mode=TwoWay}"  />
<TextBlock Text="{Binding ID,Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
代码里的
this.dropBookID.DisplayMemberPath = "FName";             
不要设

热点排行