我遇到了多重绑定WPF AutoCompleteBox的问题。我是多绑定到FirstNameTextBox和LastNameTextBox文本属性,如图所示和MainWindow。在我的转换器中,我正在尝试访问始终返回Nothing的AutoCompleteBox.Text。我也尝试过SelectedItem,并且没有返回任何内容而不是用户输入的文本。有什么建议?感谢
<my1:AutoCompleteBox IsTextCompletionEnabled="True" FilterMode="StartsWith" TextBoxBase.TextChanged="CompanySearchTBox_TextChanged" Height="23" VerticalAlignment="Center" HorizontalAlignment="Stretch" Name="companyNameTBox" Grid.Row="6" Grid.ColumnSpan="2" Margin="119,0,1,0">
<my1:AutoCompleteBox.Text>
<MultiBinding Converter="{StaticResource multiTextBoxConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.Bindings>
<Binding ElementName="firstNameTBox" Path="Text" />
<Binding ElementName="lastNameTBox" Path="Text" />
<Binding Path="DataContext" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
</MultiBinding.Bindings>
</MultiBinding>
</my1:AutoCompleteBox.Text>
</my1:AutoCompleteBox>
在我的转换器中 我从参数中获取对MainWindow的引用,并尝试获取始终返回Nothing的AutoCompleteBox.Text。有什么建议?感谢
mainWindow = CType(values.ElementAt(2), MainWindow)
_logicalName = mainWindow.companyNameTBox.Text
更新了MultiTextBoxConverter.Convert
Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
Dim firstname = values.ElementAt(0).ToString
Dim lastname = values.ElementAt(1).ToString
Dim mainWindow As MainWindow, _logicalName As String = Nothing
Dim clientInfo As Jewellery.ClientInfo = Nothing
If Not TryCast(values.ElementAt(2), MainWindow) Is Nothing Then
mainWindow = CType(values.ElementAt(2), MainWindow)
clientInfo = CType(mainWindow.FormGrid.DataContext, Jewellery.ClientInfo)
If Not clientInfo Is Nothing Then
If mainWindow.IsChecked = True Then
_logicalName = firstname + " " + lastname
Else
_logicalName = mainWindow.companyNameTBox.Text
End If
End If
If Not clientInfo Is Nothing Then clientInfo.Company.CompanyName = _logicalName
End If
Return _logicalName
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Dim obj(3) As Object
obj(0) = Binding.DoNothing
obj(1) = Binding.DoNothing
obj(2) = Binding.DoNothing
Return obj
End Function
基本上它正在测试是否选中了复选框,如果为true,则返回_logicalName = FirstName +“”+ LastName,否则返回AutoCompleteBox.Text
解决方案
Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Dim obj(3) As Object
obj(0) = Binding.DoNothing
obj(1) = Binding.DoNothing
****obj(2) = Binding.SourceUpdatedEvent****
Return obj
End Function
<!-- Added this as 3rd parameter to Multibinding code above -->
<Binding RelativeSource="{RelativeSource Self}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Path="Text" />