2014-11-08 1 views
0

현재 WPF에 대해 조금 배우고 있지만 DataBindings를 이해하지 못하고 있습니다. StackOverflow 및 MSDN에서 찾은 많은 솔루션을 시도했지만 아무도 내가 원하는 것을 수행하지 않습니다.데이터 자체 제작 클래스의 속성에 바인딩

C# -Class :

나는 다음과 같은 구조를 가지고

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace TestDataBindings 
{ 
    internal class TextStore 
    { 
     internal ObservableCollection<string> Collection { get; set; } 
     internal string Text { get; set; } 

     internal TextStore() 
     { 
      this.Text = "Hello World!!!!"; 
      this.Collection = new ObservableCollection<string> { "hello", "World", "!!!!" }; 
     } 
    } 
} 

XAML :

<Window x:Class="TestDataBindings.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:TestDataBindings" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:TextStore x:Key="TextStorage"></local:TextStore> 
    </Window.Resources> 
    <Grid> 
     <TextBox HorizontalAlignment="Left" Height="23" Margin="160,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Source={StaticResource TextStorage}, Path=Text}"/> 
     <ListBox HorizontalAlignment="Left" Height="100" Margin="305,73,0,0" VerticalAlignment="Top" Width="100" ItemsSource="{Binding Source={StaticResource TextStorage}, Path=Collection}"/> 

    </Grid> 
</Window> 

지금은 안녕하세요을 기대 !!!! 내 TextBox와 내 ListBox에 팝하는 세 개의 문자열 안에 나타나지만 그 중 어떤 것도 일어나지 않습니다. 아무도 내가 여기서 뭘 잘못하고 있다고 말할 수 있습니까?

+0

에만 공용 속성에 바인딩 할 수 있습니다. 내부에서 공개로 변경해보십시오. 클래스와 생성자는 여전히 내부 일 수 있습니다. – ShyKnee

+0

대단히 감사합니다. 한 번에 많은 문제를 해결했습니다. 공공재에만 바인딩 할 수 있다고 알려주는 문서가 있습니까? 나는 힌트를 찾기 위해 많은 자습서를 읽었지 만, 당신은 공공 재산에 묶을 수 없다는 것을 결코 발견하지 못했습니다. 정말 고마워요. –

+0

[Heres some] (http://msdn.microsoft.com/en-us/library/ms743643%28v=vs.110%29.aspx#binding_sources). 또한 출력 창은 일반적으로 문제를 추적하는 데 도움이됩니다. 그것은 나에게 많은 시간을 절약 해주었습니다. – ShyKnee

답변

0

먼저는 TextStore 클래스의 인스턴스를 사용하여 MainWindow를의 DataConetxt을 설정해야합니다

var textStore = new TextStore(); 
this.DataContext = textStore; 

다음 UI가 그들이 속성에 수행되었던 그 변경에 대한 통지를하기 위해서는 에 바인딩, 당신은에서 INotifyPropertyChanged 인터페이스를 구현해야하고, 각 속성 setter에서 OnPropertyChanged를 메서드를 호출해야합니다

internal class TextStore : INotifyPropertyChanged 
{ 
    private ObservableCollection<string> _collection; 
    public ObservableCollection<string> Collection 
    { 
     get 
     { 
      return _collection; 
     } 
     set 
     { 
      if (_collection == value) 
      { 
       return; 
      } 

      _collection = value; 
      OnPropertyChanged(); 
     } 
    } 
    private string _text; 
    public string Text 
    { 
     get 
     { 
      return _text; 
     } 

     set 
     { 
      if (_text == value) 
      { 
       return; 
      } 

      _text = value; 
      OnPropertyChanged(); 
     } 
    } 

    internal TextStore() 
    { 
     this.Text = "Hello World!!!!"; 
     this.Collection = new ObservableCollection<string> { "hello", "World", "!!!!" }; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

문제는 실제로 내부 키워드였습니다. 공개로 변경했는데 문제없이 바인딩이 작동했습니다. 실제로 GUI에 변경 사항을 적용하려는 경우 PropertyChanged 이벤트를 구현하는 방법을 알고 있지만 어쨌든 고맙습니다. –

관련 문제