使用 IMultiValueConverter 时出现 DependencyProperty.UnsetValue
我创建了一个简单的转换器来连接 WPF 应用程序中四个文本框的文本。
这是转换器:
public class FourString:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0}{1}{2}{3}", values[0], values[1], values[2], values[3]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] { };
}
}
在 Xaml 中我使用此代码:
<local:FourString x:Key="converter"/>
<TextBox Grid.ColumnSpan="4" Margin="95,7.5,71.25,3.75" Name="CodeBoatTxt" >
<TextBox.Text>
<MultiBinding Converter="{StaticResource converter}" >
<Binding ElementName="CountryStringaTxt" Path="Text" />
<Binding ElementName="CityStringaTxt" Path="Text" />
<Binding ElementName="ServiceStringaTxt" Path="Text" />
<Binding ElementName="DurationStringaTxt" Path="Text" />
</MultiBinding>
</TextBox.Text>
</TextBox>
在调试时,此错误出现在 CodeBoatTxt 文本框中:“DependecyProperty.UnsetValue”。
我的转换器出了什么问题?
I created a simple Converter to concatenate the text of four TextBoxes in my WPF app.
Here is the Converter:
public class FourString:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0}{1}{2}{3}", values[0], values[1], values[2], values[3]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] { };
}
}
in Xaml I use this code:
<local:FourString x:Key="converter"/>
<TextBox Grid.ColumnSpan="4" Margin="95,7.5,71.25,3.75" Name="CodeBoatTxt" >
<TextBox.Text>
<MultiBinding Converter="{StaticResource converter}" >
<Binding ElementName="CountryStringaTxt" Path="Text" />
<Binding ElementName="CityStringaTxt" Path="Text" />
<Binding ElementName="ServiceStringaTxt" Path="Text" />
<Binding ElementName="DurationStringaTxt" Path="Text" />
</MultiBinding>
</TextBox.Text>
</TextBox>
When in debug, this error appears in the CodeBoatTxt textbox: "DependecyProperty.UnsetValue".
What is wrong with my converter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
Binding
有效但尚未设置其值时,DependencyProperty.UnsetValue
会传递到转换器中。我会单独检查包含MultiBinding
的Binding
并确保它们正确。DependencyProperty.UnsetValue
is passed into the converter when aBinding
is valid, but does not have its value set yet. I would check theBinding
s comprising yourMultiBinding
in isolation and ensure they are correct.