多重绑定中的DependencyProperty.unsetValue [英] DependencyProperty.unsetValue in a multibinding
问题描述
我有一个listView项是 Book实例,当我单击一本书时,组合框应显示其关键字;实际上,这有点棘手:组合框包含所有书籍(已删除重复项)的所有关键字的列表(comboboxItems为复选框),并且选中了所选书籍的所有关键词。
是多重绑定:
I have a listView of items which are 'Book' instances, and when I click on a book the combobox should display its keywords; in fact it's a little trickier : the combobox contains the list of all the keywords of all books (duplicates removed)(the comboboxItems are checkboxes), and those of the selected book are checked. here is the multibinding:
<ComboBox
x:Name="cbb_Keywords"
Grid.Column="2"
Width="300"
Margin="5,0,0,0"
HorizontalAlignment="Left"
ItemsSource="{Binding Source={StaticResource AllBooks}}"
DataContext="{Binding ElementName=listBoxBooks,Path=SelectedItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Width="200">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource TextInListTrueFalseConverter}" >
<Binding Path="KeywordsForTextbox"></Binding>
<Binding RelativeSource="{RelativeSource Self}" Path="Content"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
当我运行程序时,当我单击一本书时似乎还可以,但出现异常当我单击组合框时:不可能从'MS.Internal.NamedObject'转换为'System.String'类型。我看到value [0]是UnsetValue。
When I run my program, is seems ok when I click on a book, but I get an exception when I click on the combobox : impossible cast from 'MS.Internal.NamedObject' to 'System.String' type. I saw that value[0] is UnsetValue.
在调试时,当我使用间谍跟踪WpfApp1.App.Books [0] .KeywordsForTextbox的值时,我的价值很高(一个字符串,它是Book [0]的关键字的列表。也许问题出在listboxBooks.SelectedItem.KeywordsForTextBox?我无法在VS中窥探 listboxBooks的值。
At debugging, when I use spies to track the value of WpfApp1.App.Books[0].KeywordsForTextbox, it gives me the good value (a string which is the list of the keywords of Book[0]. maybe the problem comes from listboxBooks.SelectedItem.KeywordsForTextBox? I can't spy in VS the value of 'listboxBooks'.
一些相关内容...
MainWindow的构造函数的开头:
some related content... the beginning of the constructor of MainWindow:
public MainWindow()
{
InitializeComponent();
listBoxBooks.ItemsSource = App.Books;
转换器的转换方法:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var check = false;
if ((values != null && values.Length == 2))
{
string listString = (string)values[0];
string wordToFind = (string) values[1];
if ((listString != null))
{
List<string> keywordsList = listString.Split(',').ToList();
if (keywordsList.Contains(wordToFind)) check = true;
}
}
return check;
}
KeywordsForTextbox方法:
the KeywordsForTextbox method:
public string KeywordsForTextbox
{
get { return string.Join(",", _keywords); }
}
最终实现AllBooks :(作为一个窗口资源)
and finally the implementation of AllBooks:(as a window resource)
<ObjectDataProvider
x:Key="AllBooks"
MethodName="listOfAllKeywords"
ObjectType="{x:Type mangmt:BookManagement}" />
谢谢。
推荐答案
Multi的第一个绑定应该是到Books ListBox中的SelectedItem。我在适当的地方添加了< CheckBox.IsChecked>
,并将Content = {Binding}添加到CheckBox中:
The first Binding of the Multi should be to be to the SelectedItem in the ListBox of Books. I have added in the <CheckBox.IsChecked>
where appropriate, and Content="{Binding}" to the CheckBox:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Width="200" Content={Binding}>
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource TextInListTrueFalseConverter}" >
<Binding ElementName=listBoxBooks, Path=SelectedItem.KeywordsForTextbox"></Binding>
<Binding RelativeSource="{RelativeSource Self}" Path="Content"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
您可能还希望向IMultiValueConverter添加一些验证以确保传递的值未设置,以避免发生异常:如果VB中的not values(0)是DependencyProperty.UnsetValue而不是values(1)是DependencyProperty.UnsetValue然后
You may also wish to add some validation to the IMultiValueConverter to make sure the passed values are not unset, to avoid an exception: If Not values(0) Is DependencyProperty.UnsetValue And Not values(1) Is DependencyProperty.UnsetValue Then
in VB.
关于选中复选框的行为,我猜这是因为ConvertBack IMultiValueConverter的方法。您可以删除抛出异常代码,并编写一种方法来将选中/未选中框的文本添加/删除到关键字列表中。
Regarding the behaviour on checking the checkbox, I am guessing this is because of the ConvertBack Method of the IMultiValueConverter. You can remove the 'Throw Exception' code, and write a method to add/remove the text of the checked/unchecked box to your keyword list.
这篇关于多重绑定中的DependencyProperty.unsetValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!