天天看點

WPF ListBoxItem在綁定對象後其中添加控件後如何再次綁定ViewModel中的Command?

在這裡我用的是資料模闆,界面展示時通過模闆選擇器進行選擇對應的模闆,選擇器暫時不進行展示,下面展示下資料模闆中的代碼如下:

<DataTemplate x:Key="chatAudioSend1"> 
        ...省略部分代碼,這裡主要展示在Item中的Button如何綁定Command
        <Border    Grid.Row="1 " 
                   Grid.Column="0"  
                    x:Name="border1" 
                    CornerRadius="1"
                    BorderBrush="#9EEA6A" 
                    BorderThickness="1" 
                    HorizontalAlignment="Right" 
                    Margin="0,2,10,2"                 
                    Background="#9EEA6A"  
                    MinHeight="30" 
                    MinWidth="100">
                     
               <Button  HorizontalAlignment="Right" 
                        Name="btnSendAudio" 
                        Margin="10,0" 
                        Content="&#xe628;" 
                        Style="{StaticResource btnAudioStyle }"
                        Tag="{Binding .}" 
                        FontFamily="../../Images/#iconfont" 
                        FontSize="20">
               </Button> 
        </Border>
        ...省略部分代碼
</DataTemplate>
           

下面展示Button的Style中如何綁定指令:

<Style TargetType="Button" x:Key="btnAudioStyle">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Foreground" Value="DarkGray" />
        <Setter Property="Command" 
                Value="{Binding DataContext.PlayAudioCommand,
                         RelativeSource={RelativeSource Mode=FindAncestor,
                                         AncestorType={x:Type UserControl}}}" 
        />
        <Setter Property="CommandParameter" 
                Value="{Binding RelativeSource={x:Static RelativeSource.Self}}" />
        <Setter Property="Template">
                <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}" >
                        <Border x:Name="PART_BUTTON_BORDER"
                                BorderBrush="{TemplateBinding Control.BorderBrush}"                         
                                BorderThickness="0" CornerRadius="3" 
                                Background="{TemplateBinding Background}">
                             <ContentPresenter
                                     Content="{TemplateBinding ContentControl.Content}"                         
                                     HorizontalAlignment="Center"         
                                     VerticalAlignment="Center" >
                             </ContentPresenter>
                        </Border>
                        </ControlTemplate>
                </Setter.Value>
       </Setter>
       <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="CornflowerBlue"/>
                        <Setter Property="Cursor" Value="Hand" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="Gray"/>
                        <Setter Property="Cursor" Value="No" />
                        <Setter Property="BorderBrush" Value="Gray" />
                    </Trigger>
       </Style.Triggers>
</Style>
           

使用{Binding DataContext.PlayAudioCommand, RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type UserControl}}},即可綁定ViewModel中的指令,CommandParameter的值也将button自身作為參數傳遞進去了。