2013-12-10 25 views
2

我有一個用戶控件,它包含一個文本框,並在usercontrol中創建了一個get/set來獲取/設置文本框的text屬性。使用綁定來設置一個用戶控件中的文本框的文本屬性 - WPF

public class OpenFileControl : UserControl 
{ 
    StackPanel sp; 
    public TextBox tb; 

    public string Text { get { return tb.Text; } set { tb.Text = value; } } 

然後我想設置基於綁定後該值 -

<gX3UserControls:OpenFileControl Text="{Binding Value}" /> 

,但我得到以下異常 A「綁定」不能在類型的「文本」屬性設置'OpenFileControl'。 '綁定'只能在DependencyObject的DependencyProperty上設置。

經過一番調查看來文本需要是一個依賴屬性,但如果我這樣做,我不能解決如何將值傳遞給文本框。

我該如何解決這個問題。

回答

0

如果您將依賴項屬性定義爲靜態屬性和實際屬性,則可以在屬性的主體中編寫任何想要的代碼。

public const string TextPropertyName = "Text"; 
public string Text 
    { 
     get 
     { 
      return (string)GetValue(TextProperty); 
     } 
     set 
     { 
      SetValue(TextProperty, value); 
     } 
    } 

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
     TextPropertyName, 
     typeof(string), 
     typeof(MyControl), 
     new UIPropertyMetadata(false)); 

在getter和setter中,你可以做類似textBox1.Text = value;但你可能會更好地使用綁定到屬性來代替。 MVVM框架經常爲這類事情做簡單的工作。您可能會發現定義ViewModel的更多成功(例如,具有適當的FielPath變量的類),並將新的UserControl的DataContext設置爲ViewModel類的實例,並使用Bindings爲您完成繁重任務。

+1

要知道,使用此語句新UIPropertyMetadata(假)將帶領你的ArgumentException,因爲字符串不能用布爾值初始化。 – Somedust

+0

非常真實,我的錯誤,null應該更好地作爲註冊命令 – Dave

4

考慮使用類似這樣的東西。

控制XAML:

<UserControl x:Class="WpfTestBench.OpenFileControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel> 
     <TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
      Path=Filename, UpdateSourceTrigger=PropertyChanged}" /> 
    </StackPanel> 
</UserControl> 

控制代碼隱藏:

using System.Windows; 

namespace WpfTestBench 
{ 
    public partial class OpenFileControl 
    { 
     public static readonly DependencyProperty FilenameProperty = 
      DependencyProperty.Register("Filename", typeof (string), typeof (OpenFileControl)); 

     public OpenFileControl() 
     { 
      InitializeComponent(); 
     } 

     public string Filename 
     { 
      get { return (string)GetValue(FilenameProperty); } 
      set { SetValue(FilenameProperty, value); } 
     } 
    } 
} 

主要XAML:

<Window x:Class="WpfTestBench.OpenFileWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wpfTestBench="clr-namespace:WpfTestBench" 
     Title="OpenFileWindow" Width="300" SizeToContent="Height"> 
    <StackPanel> 
     <wpfTestBench:OpenFileControl x:Name="In" Filename="{Binding SelectedFilename, UpdateSourceTrigger=PropertyChanged}" /> 
     <wpfTestBench:OpenFileControl x:Name="Out" Filename="{Binding ElementName=In, Path=Filename}" /> 
    </StackPanel> 
</Window> 

主代碼隱藏:

namespace WpfTestBench 
{ 
    public partial class OpenFileWindow 
    { 
     public OpenFileWindow() 
     { 
      InitializeComponent(); 

      DataContext = this; 
     } 

     public string SelectedFilename { get; set; } 
    } 
} 

執行結果(輸入後在第一控制的東西):

Result

+1

+1上的最終參數,並且如果relativesource綁定不是您的朋友,您也可以執行elementname綁定:< TextBox Text =「{Binding ElementName = uc,Path = Filename}」/> :) – blindmeis

相關問題