2017-05-17 103 views
-1

我想將一些整數轉換爲DataGrid列的ReadOnly值。對於這一點,我做了以下內容:C#/ WPF - 轉換器不會被調用?

namespace TanulmanyiRendszer.Admin.ViewModel 
{ 
    public class GradeToReadOnlyConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      Boolean IsReadOnly = (Int32.Parse((String)value) < 2) ? true : false; 
      return IsReadOnly; 
     } 
     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return null; 
     } 
    } 
} 

XAML瀏覽

<Window x:Class="TanulmanyiRendszer.Admin.View.MainWindow" 
    <!-- ETC --> 
    xmlns:viewModel="clr-namespace:TanulmanyiRendszer.Admin.ViewModel" 
    Title="Courses" Height="600" Width="500"> 
     <Window.Resources> 
      <viewModel:GradeToReadOnlyConverter x:Key="converter" /> 
     </Window.Resources> 
     <!-- ETC --> 
     <DataGrid Name="studentGrid" Grid.Row="3" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding Students, Mode=TwoWay}" > 
      <DataGrid.Columns> 
       <DataGridTextColumn IsReadOnly="{Binding StudentGrade, Converter={StaticResource converter}}" Header="Student's grade" Binding="{Binding StudentGrade}"/> 
      </DataGrid.Columns> 
     </DataGrid> 
</Window> 

然而,這並沒有在所有的工作。轉換器永遠不會被調用。我在這裏錯過了什麼?

+2

但'綁定=「{綁定StudentGrade}」'工作? – Clemens

+0

@Clemens是的,exaclty。等級顯示出來,但我可以編輯它們而不考慮價值。 –

+0

檢查輸出窗口,您必須得到此錯誤:找不到目標元素的控制FrameworkElement或FrameworkContentElement。 BindingExpression:路徑= StudentGrade;的DataItem = NULL;目標元素是'DataGridTextColumn'(HashCode = 15006601);目標屬性是'IsReadOnly'(類型'布爾')。相同的原因和解決方案可以在這裏找到:http://stackoverflow.com/questions/7660967/wpf-error-cannot-find-governing-frameworkelement-for-target-element –

回答

0

但是,這根本不起作用。

這是在這種情況下的預期行爲。這背後的原因是Datagrid列不是可視樹的一部分,這就是Datagrid列沒有連接到Datagrid的Datacontext的原因。因此與Datagrid Column的IsReadOnly屬性綁定將不起作用。此方案的一個好的解決方案(黑客/解決方法)可以在here找到。

讓我知道你是否需要任何幫助與解決方案提供的鏈接。

1
<DataGridTextColumn Header="Student's grade" Binding="{Binding StudentGrade}"> 
    <DataGridTextColumn.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="IsHitTestVisible" Value="{Binding StudentGrade, Converter={StaticResource converter}}" /> 
     </Style> 
    </DataGridTextColumn.CellStyle> 
</DataGridTextColumn> 

試着改變DataGridCell的風格。 Setter屬性可能是'IsHitTestVisible'或'IsEnabled'

0

我在這裏丟失了什麼?

一個DataGridTextColumn沒有繼承任何DataContext這意味着你不能將它的IsReadOnly屬性直接綁定到StudentGrade源屬性的事實。

請參考@Thomas Levesque的的博客文章有關更多信息,以及如何你可能通過使用BindingProxy類,從Freezable繼承和捕捉DataContext解決您的問題:https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

雖然只是爲該列定義自定義EditingElementStyle會更容易。試試這個:

<DataGridTextColumn IsReadOnly="{Binding StudentGrade, Converter={StaticResource converter}}" 
        Header="Student's grade" Binding="{Binding StudentGrade}"> 
    <DataGridTextColumn.EditingElementStyle> 
     <Style TargetType="TextBox"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding StudentGrade, Converter={StaticResource converter}}" Value="True"> 
        <Setter Property="BorderThickness" Value="0" /> 
        <Setter Property="IsEnabled" Value="False" /> 
        <Setter Property="Background" Value="Transparent" /> 
        <Setter Property="TextWrapping" Value="Wrap" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGridTextColumn.EditingElementStyle> 
</DataGridTextColumn>