2011-01-25 40 views
0

我有一个文本框,它使用多个绑定usingStringFormat ...如下所示。 但它显示的默认值 {DependencyProperty.UnsetValue},{} DependencyProperty.UnsetValue如何避免文本框中的默认文本?

如何避免这种情况?

<StackPanel Orientation="Horizontal"> 
        <TextBlock Width="70" Text="Name:" Margin="5,2,2,2"></TextBlock> 
        <TextBox Width="160" DataContext="{Binding }" IsReadOnly="True" Margin="2"> 
         <TextBox.Text> 
          <MultiBinding StringFormat="{}{0},{1}"> 
           <Binding Path="LastName"/> 
           <Binding Path="FirstName"/> 
          </MultiBinding> 
         </TextBox.Text> 
        </TextBox> 
</StackPanel> 

请帮帮我。

+0

不清楚。试着看看VS中的输出窗口,在那里你可以看到所有的绑定错误。 DataContext中绑定了什么=“{Binding}”? – SeeSharp 2011-01-25 04:47:34

回答

0

您绑定的对象有问题。我从头创建了一个从DependencyObject继承的Person类的应用程序。我没有设置第一个和最后一个名称属性,我没有看到DependencyProperty.UnsetValue,而是一个只有逗号的空白TextBox。

(一般情况下,你不应该用你的业务对象的依赖属性无妨。坚持INotifyPropertyChanged的并保存自己吨头痛。)

邮政代码到你绑定的对象,也许我可以当场问题。

public class Person : DependencyObject 
{ 

    public static readonly DependencyProperty FirstNameProperty = DependencyProperty.Register("FirstName", typeof(string), typeof(Person), new FrameworkPropertyMetadata()); 

    public string FirstName { 
     get { return (string)GetValue(FirstNameProperty); } 
     set { SetValue(FirstNameProperty, value); } 
    } 

    public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(Person), new FrameworkPropertyMetadata()); 

    public string LastName { 
     get { return (string)GetValue(LastNameProperty); } 
     set { SetValue(LastNameProperty, value); } 
    } 

} 

-

<TextBox IsReadOnly="True"> 
    <TextBox.Text> 
     <MultiBinding StringFormat="{}{1}, {0}"> 
      <Binding Path="FirstName" /> 
      <Binding Path="LastName" /> 
     </MultiBinding> 
    </TextBox.Text> 
</TextBox> 

-

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     this.InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     var p = new Person(); 
     //p.FirstName = "Josh"; 
     //p.LastName = "Einstein";   
     DataContext = p; 
    } 
} 
0

只是给fallbackvalue =“”,看看

<TextBox.Text> 
       <MultiBinding StringFormat="{}{0},{1}"> 
        <Binding Path="LastName" FallbackValue=""/> 
        <Binding Path="FirstName" FallbackValue=""/> 
       </MultiBinding> 
      </TextBox.Text> 

如果绑定不成功,即路径绑定源没有找到或值转换器,如果有的话,失败,一个DependencyProperty。如果您定义了一个,则返回UnsetValue,然后将目标属性设置为FallbackValue。