2011-07-21 16 views
3

私は同じタイプのオブジェクトのリストに動的にバインドされたListViewを持っています。WPF - セル行のチェックボックスは読み取り専用ですか?

オブジェクトにはブール値があります。

この特定のプロパティの "true"と "false"の代わりにチェックボックスを表示するListView列があります。

このチェックボックスを読み取り専用に設定する方法はありますか?さもなければ、コードの中でメソッドを実行する "checked"と "unchecked"というイベントのこの特定の行からクリックが来ていることを伝える方法がありますか?

ありがとうございます!

答えて

16

IsHitTestVisibleとFocusableをfalseに設定すると、すべてのコントロールを読み取り専用にすることができます。

XAML:背後

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"> 

    <StackPanel> 
     <ListView ItemsSource="{Binding}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" /> 
        <GridViewColumn Header="Is Valid"> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <CheckBox IsChecked="{Binding Path=IsValid}" IsHitTestVisible="False" Focusable="False" /> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </StackPanel> 

</Window> 

コード:

using System.Collections.Generic; 

namespace WpfApplication1 
{ 
    public partial class Window1 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      List<DataItem> data = new List<DataItem>(); 
      data.Add(new DataItem() { Name = "AAA", IsValid = true }); 
      data.Add(new DataItem() { Name = "BBB" }); 
      DataContext = data; 
     } 

     public class DataItem 
     { 
      public string Name { get; set; } 
      public bool IsValid { get; set; } 
     } 
    } 
} 
+0

ありがとうございます!それは私が探していたもの。 – Rushino

0

あなたはRenderPressedを削除し、あなたのDataContext財産の代わりTemplateBindingIsCheckedをバインドするCheckBoxControlTemplateのスタイルを変更することができます。編集したテンプレートは、IsChecked="{Binding MyBoolean}"を探し、それをあなたの財産に変更します。

<LinearGradientBrush x:Key="CheckRadioFillNormal"> 
    <GradientStop Color="#FFD2D4D2" Offset="0"/> 
    <GradientStop Color="#FFFFFFFF" Offset="1"/> 
</LinearGradientBrush> 
<LinearGradientBrush x:Key="CheckRadioStrokeNormal"> 
    <GradientStop Color="#FF004C94" Offset="0"/> 
    <GradientStop Color="#FF003C74" Offset="1"/> 
</LinearGradientBrush> 
<Style x:Key="EmptyCheckBoxFocusVisual"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Rectangle Margin="1" SnapsToDevicePixels="true" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
<Style x:Key="CheckRadioFocusVisual"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
<Style x:Key="ReadonlyCheckBox" TargetType="{x:Type CheckBox}"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="Background" Value="{StaticResource CheckRadioFillNormal}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource CheckRadioStrokeNormal}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyCheckBoxFocusVisual}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type CheckBox}"> 
       <BulletDecorator Background="Transparent" SnapsToDevicePixels="true"> 
        <BulletDecorator.Bullet> 
         <Microsoft_Windows_Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsChecked="{Binding MyBoolean}" RenderMouseOver="{TemplateBinding IsMouseOver}"/> 
        </BulletDecorator.Bullet> 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </BulletDecorator> 
       <ControlTemplate.Triggers> 
        <Trigger Property="HasContent" Value="true"> 
         <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/> 
         <Setter Property="Padding" Value="2,0,0,0"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
0

透明なTextBlockをCheckBoxに配置します。 CheckBoxを無効にするには、 "IsEnabled"を設定し、このTextBlockのツールヒントのコンテンツを使用します。 以下の例:

<DataGridTemplateColumn Header="Hdr" Width="34" > 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
     <Grid> 
      <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="False" IsChecked="{Binding IS_CHECKED}"/> 
      <TextBlock> 
       <ToolTipService.ToolTip> 
       <ToolTip Content="{Binding TOOL_TP}"/> 
       </ToolTipService.ToolTip> 
     </TextBlock> 
     </Grid> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
関連する問題