2014-10-02 5 views
1

DataGrid 및 TextBox가 포함 된 사용자 지정 UserControl이 있는데,이 요소에 대해 DependencyProperties를 사용하여 데이터 바인딩을 시도하고 있습니다. 바인딩은 DataGrid에는 좋지만 TextBox에는 적합하지 않습니다.DependencyProperty를 사용하여 TextBox의 바인딩을 설정하는 방법

코드 :

public static readonly DependencyProperty BuiDataProperty = DependencyProperty.Register("BuiData", typeof(IEnumerable), typeof(BelastingTab), new PropertyMetadata(default(IEnumerable), BuiDataChanged)); 

private static void BuiDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var Object = d as BelastingTab; 
    if (Object == null) return; 
    Object.BuiDataDataSourceChanged(d, e); 
} 

private void BuiDataDataSourceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
{ 
    BuiDataTabel.ItemsSource = dependencyPropertyChangedEventArgs.NewValue as IEnumerable; 
} 

public IEnumerable BuiData 
{ 
    get { return (IEnumerable)GetValue(BuiDataProperty); } 
    set { SetValue(BuiDataProperty, value); } 
} 

그리고 주요 XAML에서

:

<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}"/> 

이것은 DataGrid에 바인딩을 설정하는 코드가 나는 텍스트 상자에 대해 같은 일에 대해 어떻게 갈 것입니까?

편집 : 이 내가 현재 가지고있는 것입니다,

홈페이지 XAML은 :

<src:BelastingTab BuiData="{Binding Path=Static.BuienRegulier[0].BuiTabel}" HerhalingsTijd="{Binding Path=Static.BuienRegulier[0].HerhalingsTijd}"/> 

이 문자열을 의미한다. UserControl을 XAML에서 : UserControl을 XAML CS에서

<TextBox Text="{Binding HerhalingsTijd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 

:

public static readonly DependencyProperty HerhalingsTijdProperty = DependencyProperty.Register("HerhalingsTijd", typeof(string), typeof(BelastingTab), new PropertyMetadata(string.Empty)); 

public string HerhalingsTijd 
{ 
    get { return (string)GetValue(HerhalingsTijdProperty); } 
    set { SetValue(HerhalingsTijdProperty, value); } 
} 

답변

2

나는 당신이 원하는 일을에서 문제를 볼 수 없습니다. 간단한 테스트 애플리케이션을 만들었습니다. 여기에 코드를 제공 할 것이고, 그것이 당신이 잘못한 것을 어떻게 든 고치는 데 도움이되기를 바랍니다.

위해 UserControl1 코드 :

public partial class UserControl1 : UserControl 
{ 
    public static DependencyProperty TxtBoxValueProperty = DependencyProperty.Register("TxtBoxValue", typeof(String), typeof(UserControl1)); 

    public String TxtBoxValue 
    { 
     get { return (String)GetValue(TxtBoxValueProperty); } 
     set 
     { 
      SetValue(TxtBoxValueProperty, value); 
     } 
    } 

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
     base.OnPropertyChanged(e); 
     if (e.Property == TxtBoxValueProperty) 
     { 
      // Do whatever you want with it 
     } 
    } 

    public UserControl1() 
    { 
     InitializeComponent(); 
    } 
} 

사용자 제어 XAML :

<StackPanel> 
    <TextBox Text="{Binding TxtBoxValue, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}, Mode=TwoWay}" Width="100" Height="50"/> 
    <TextBox></TextBox> 
</StackPanel> 

메인 창 XAML :

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:WpfApplication1" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
<Grid> 
    <local:UserControl1 TxtBoxValue="{Binding TextBoxValue, Mode=TwoWay}"></local:UserControl1> 
</Grid> 

메인 창 코드 숨김

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    CancellationTokenSource cTS; 
    CancellationToken cT; 

    private String _textBoxValue; 
    public String TextBoxValue 
    { 
     get { return _textBoxValue; } 
     set 
     { 
      _textBoxValue = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("TextBoxValue")); 
      } 

      if (_textBoxValue.Contains("enough")) 
      { 
       cTS.Cancel(); 
      } 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     cTS = new CancellationTokenSource(); 
     cT = cTS.Token; 
     Task.Factory.StartNew(ChangeTextBoxValue, cT); 
    } 

    public void ChangeTextBoxValue() 
    { 
     while (true) 
     { 
      Random rnd = new Random(DateTime.Now.Millisecond); 
      TextBoxValue = (rnd.NextDouble() * 1000.0).ToString(); 
      Thread.Sleep(10000); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

필자는이 방법을 매우 빨리 작성하여 일반적으로 (ViewModelBase에 넣는 것을 제외하고) 어떻게 사용했는지 확인합니다.

이 방법으로 문제가 해결되지 않으면 질문을 이해하지 못했거나 매우 구체적인 것이 있지만 의심 스럽습니다.

+0

내가 가지고있는 편집 작업을 게시했습니다. 어떤 작업이 이루어지지 않았습니까? – user3692104

+0

RelativeSource를 추가 한 후에 작동합니까? – user3692104

+1

사용자 컨트롤에서 클래스 속성 뒤에있는 코드에 액세스 할 수있는 유일한 방법이기 때문에 상대 소스가 필요합니다. 컨트롤에 데이터 컨텍스트가 없으므로 데이터 컨텍스트를 Self로 만들거나 상대 소스를 사용하여 코드 배후에서 속성에 액세스하므로 바인딩에서 속성을 가져올 곳을 알 수 있습니다. 이걸 완전히 이해하려면 약간의 실험과 독서가 필요합니다. – XMight

관련 문제