2016-05-26 9 views
0

建物のリストがあります。建物は独自のクラス(core)を持ち、ObservableCollectionに保存されます。ビルディングがリストに表示されますが、リストに表示されている変数を変更すると、その変数はxamlでは変更されません。ここでデータが変更されたときにWPF ListBoxでのバインディングがTextBoxを更新しない

は、クラスのソースです:WPFの

public class core 
    { 
     // core ---------------------------------------------- 
     static public ObservableCollection<core> cores { get; set; } = new ObservableCollection<core>(); 
     string namef = "building"; 

public core() 
     { 
      cores.Add(this); 
     } 

public string Namef 
     { 
      get { return namef; } 
      set { namef = value; } 
     } 
} 

- XAML:

<Page x:Class="idle.pages.game" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:idle.pages" 
    mc:Ignorable="d" 
    d:DesignHeight="454.259" d:DesignWidth="757.012" 
    Title="game"> 

<Grid Margin="0" x:Name="gridx"> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <TabControl x:Name="tabControl"> 
     <TabItem Header="Budovy"> 
      <Grid Background="#FFE5E5E5"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="300"/> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions> 
       <ListBox x:Name="listBox"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <Grid Margin="10,7" HorizontalAlignment="Stretch" Height="100"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="100" /> 
            <ColumnDefinition Width="*" /> 
           </Grid.ColumnDefinitions> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="*"/> 
            <RowDefinition Height="*"/> 
            <RowDefinition Height="*"/> 
            <RowDefinition Height="*"/> 
           </Grid.RowDefinitions> 
           <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" CornerRadius="2"/> 
           <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Namef}" /> 
          </Grid> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
       <Frame x:Name="frame" Content="Frame" Grid.Column="1" NavigationUIVisibility="Hidden" Source="/idle;component/pages/Building.xaml"/> 


      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 

とWPFの - のC#:

public partial class game : Page 
{ 
    public game() 
    { 
     InitializeComponent(); 
     new core() { Namef = "b1"}; 
     new core() { Namef = "b2"}; 
     new core() { Namef = "b3"}; 

     core.start(this); 


     listBox.ItemsSource = xxx; 
    } 

    public ObservableCollection<core> xxx { get; set; } 
} 

確認イム、変数ことクラスの内部は変更されていますが、xamlは変更されていません。どうしましたか?

+0

:たとえば、あなたがNamefへの変更を確認したい場合は、このようにそれを実装する必要があると思います新しいコアをxxx – SWilko

答えて

0

INotifyPropertyChangedインターフェイスを実装し、変更可能な(UIで更新したい)すべてのプロパティの設定者にイベントを呼び出す必要があります。あなたはまた、追加していない `xxx`例えば、XXX =新しいのObservableCollection ()の作成したインスタンスをhaventは

public class core : INotifyPropertyChanged 
{ 
    static public ObservableCollection<core> cores { get; set; } 
     = new ObservableCollection<core>(); 

    string namef = "building"; 

    public core() 
    { 
     cores.Add(this); 
    } 

    public string Namef 
    { 
     get { return namef; } 
     set 
     { 
      if(namef == value) return; 

      namef = value; 
      OnPropertyChanged("Namef"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(String propertyName) 
    { 
     if (PropertyChanged == null) return; 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

xaml shoudと同じにするか、または何か変更する必要がありますか? –

+0

ありがとう、それは完璧に働いた:-) –

関連する問題