![](/img/trans.png)
[英]WPF Bind Checkbox IsChecked to other Checkboxes (IsChecked || !IsEnabled)
[英]Bind IsChecked of a CheckBox to a method
是否可以將復選框的IsChecked屬性綁定到自定義方法?
我創建了綁定到對象集合的復選框列表。 我有第二個對象集合,它是第一個對象的子集。 我想將復選框的IsChecked屬性綁定到確定對象是否包含在第二個列表中的方法
編輯:
<ListBox Height="auto" HorizontalAlignment="Stretch" Name="listBox" VerticalAlignment="Stretch" Width="auto" ItemsSource="{Binding DataSources}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="CheckBoxZone"
Content="{Binding Name}"
Tag="{Binding Id}"
Margin="0,5,0,0"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您可以將復選框Command屬性綁定到模型上的ICommand。 這意味着每次更改檢查都會調用該命令。
例:
<CheckBox Name="CheckBoxZone"
Content="{Binding Name}"
Tag="{Binding Id}"
Margin="0,5,0,0"
Command={Binding CheckBoxChangedCommand}
/>
您可以通過MultiBinding結合多值轉換器將IsChecked
綁定到數據對象和子集集合,該多值轉換器將轉換為bool
(或IsChecked
Nullable<bool>
)值:
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource ObjectInListConverter}" Mode="OneWay">
<Binding />
<Binding Source="{StaticResource SubsetCollection}" />
</MultiBinding>
</CheckBox.IsChecked>
轉換器:
class ObjectInListConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
IList subset = values[1] as IList;
Nullable<bool> result = subset.Contains(values[0]);
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
除了Pop Catalin的答案之外,您還需要將IsChecked綁定到VM中的屬性,並在執行命令時修改該VM屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.