2017-03-04 36 views
0

enter image description here如何在GridCell UWP中爲複選框位置寫入樣式?

我試圖寫一個通過將目標類型複選框放在網格單元格複選框的風格,但它也適用風格進行過濾複選框。任何人都可以建議我只寫格子單元格的複選框。 隨函附上上面的圖片,我有寫一個風格只在網格單元,而不是適用於所有

我嘗試了用這種方式

<Page.Resources> 
     <Style TargetType="CheckBox"> 
      <Setter Property="Background" Value="Red"/> 
     </Style> 
    </Page.Resources> 
    <Page.DataContext> 
     <local:ViewModel/> 
    </Page.DataContext> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <my:SfDataGrid ItemsSource="{Binding Orders}" AllowFiltering="True" /> 
    </Grid> 
+0

你能發佈,請你嘗試了一些什麼碼? *並非所有問題都能從代碼中受益。但是,如果你的問題是你寫的代碼,你應該包括一些。但不要只複製你的整個程序!* http://stackoverflow.com/help/how-to-ask –

+0

這很奇怪。我用你的代碼在我身邊測試。我身邊沒有任何影響。無論單元格中還是filterControl中的checkBox,它們都沒有紅色背景。 – Skyblue

回答

0

你必須使用Column屬性加載的複選框的DataGridCell並檢查其值DisplayIndex值並編寫一個DataTrigger

樣品的方法:

<DataGrid ...> 
     <DataGrid.Resources> 
      <Style TargetType="CheckBox"> 
       <Setter Property="Background" Value="Green"/> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource AncestorType=DataGridCell, Mode=FindAncestor}}" Value="0"> 
         <Setter Property="Background" Value="Red"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </DataGrid.Resources> 
    </DataGrid> 
+0

您好Anjum,感謝您查看我的查詢,請您分享這個查詢在UWP的建議。 – James

0

確定。 @BalamuruganR我爲你做了一個簡單的代碼示例。使用GridTemplateColumn併爲您的自定義樣式指定一個「x:Key」。請檢查下面的代碼示例:

<Page.Resources> 
    <Style x:Key="CheckBoxStyle1" TargetType="CheckBox"> 
     <Setter Property="Background" Value="Red" /> 
    </Style> 
    <DataTemplate x:Key="cellTemplate"> 
     <CheckBox Content="DataGrid" IsChecked="{Binding Flag}" Style="{StaticResource CheckBoxStyle1}"></CheckBox> 
    </DataTemplate> 
</Page.Resources> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

    <syncfusion:SfDataGrid x:Name="sfGrid" Grid.Column="0" 
           AllowGrouping="True" 
           AutoExpandGroups="True" 
           ShowGroupDropArea="True" 
           AllowEditing="True" 
           AllowFiltering="True" 
           AutoGenerateColumns="False" 
           ItemsSource="{Binding UserDetails}"> 
     <syncfusion:SfDataGrid.Columns> 
      <syncfusion:GridTextColumn MappingName="UserId" /> 
      <syncfusion:GridTextColumn MappingName="Name" /> 
      <syncfusion:GridDateTimeColumn MappingName="DateofBirth" /> 
      <syncfusion:GridNumericColumn MappingName="ContactNo" /> 
      <syncfusion:GridTemplateColumn MappingName="Flag" CellTemplate="{StaticResource cellTemplate}" /> 
     </syncfusion:SfDataGrid.Columns> 
    </syncfusion:SfDataGrid> 
</Grid> 

enter image description here

+0

HI Anjum,在您提供的代碼示例中,樣式應用於單元格中的整個內容。但我的查詢是將該樣式僅應用於複選框而不是整個單元格。 – James