DataGridには、DataGridviewcheckboxcolumnで表されるIsEnabledプロパティを含む項目のリストが表示されます。 チェックするチェックボックスの数を同時に5個に制限したいと考えています。WPF DataGridviewcheckboxColumn内のチェックされた行の数を制限します。
どうすればいいですか?
編集:
私は今れる多を使用しているやっている:コンバータは、アイテムの「でIsEnabled」プロパティオブジェクトと入力値などの項目のリスト自体を受け入れます。このようなMyConverterlooks内
<DataGrid ItemsSource="{Binding MyItems}" AutoGenerateColumns="false"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="false">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="" Binding="{Binding Path=IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridCheckBoxColumn.CellStyle>
<Style>
<Setter Property="CheckBox.IsEnabled" >
<Setter.Value>
<MultiBinding Converter="{Utilities:MyConverter}">
<Binding Path="IsEnabled" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="DataContext.MyItems" RelativeSource="{RelativeSource AncestorType=UserControl}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
...
変換機能:予想通り
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var val1 = (bool)values[0];
int numSelected = 0;
if (values[1] != null && values[1] is ObservableCollection<MyTestItem>)
{
var list = (ObservableCollectionBase<MyTestItem>)values[1];
foreach (MyTestItem mti in list)
{
if (mti.IsEnabled)
numSelected++;
}
}
else
{
return false;
}
return val1 ? val1 : (numSelected < 5);
}
これは動作します(5以下のチェックボックスを同時に選択することができ、他のすべてが無効になっている)が、私は入れません以下のような警告:
System.Windows.Data Warning: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.MyItems; DataItem=null; target element is 'DataGridCell' (Name=''); target property is 'IsEnabled' (type 'Boolean')
は、私はまた、データグリッドの名前を設定し、結合して「のElementName」を使用しようとしましたが、動作が正しいですが、私は、同じ警告を得続けます。
なぜこれらの警告が表示されますか?
ありがとうございますが、私はMVVMを使用しているので、バインドでこれを解決したいと思います。だから、コードビハインドでcheckedイベントハンドラを使用して私が探しているものではありません。 – tabina