2016-02-12 82 views
0

我試着解決這個問題,看看這裏堆棧溢出的幾個可能的解決方案,但唉,我一直無法解決這個問題。從數據綁定列表框中選定的項目的WPF嵌套屬性


TL; DR版本:

問題:

使用數據綁定顯示的RPG角色,這對他們的屬性的嵌套屬性列表,列表框。由於嵌套屬性和數據綁定的限制,我無法顯示屬性。 下面的代碼與問題有關。


我有一個listBox有一個數據綁定,控制列表中顯示的內容。數據綁定使用ObservableCollection作爲列表包含的對象的列表。所有這些工作都很好,但與手頭的問題有關。

listBox databinding在每個元素中都有幾個嵌套的屬性,我想要在表單中顯示和更改,但是我無法使嵌套的數據綁定正常工作。

這是列表框XAML:

<ListBox x:Name="listCharacters" Margin="2,0" ItemsSource="{Binding}" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionChanged="listCharacters_SelectionChanged"> 
    <ListBox.ItemTemplate> 
     <DataTemplate DataType="{x:Type local:Character}" x:Name="Symchar"> 
      <Grid Width="125" HorizontalAlignment="Left" Background="{x:Null}"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="18"/> 
        <RowDefinition Height="12"/> 
        <RowDefinition Height="16"/> 
       </Grid.RowDefinitions> 
       <Image Panel.ZIndex="5" HorizontalAlignment="Right" VerticalAlignment="Top" Height="16" Width="16" Margin="0,2,0,0" Source="Resources/1454889983_cross.png" MouseUp="DeleteCharacter" /> 
       <TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" FontWeight="Bold" FontSize="13.333" Grid.Row="0" TextTrimming="CharacterEllipsis" Padding="0,0,16,0" /> 
       <TextBlock Text="{Binding RealRace.Label, UpdateSourceTrigger=PropertyChanged}" FontSize="9.333" Grid.Row="1" FontStyle="Italic" /> 
       <TextBlock FontSize="9.333" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top"> 
        <Run Text="{Binding RealClass.Archetype.Label, UpdateSourceTrigger=PropertyChanged}"/> 
        <Run Text=" - "/> 
        <Run Text="{Binding RealClass.Label, UpdateSourceTrigger=PropertyChanged}"/> 
       </TextBlock> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

和設置列表框的ItemSource:

this.listCharacters.ItemsSource = CharacterList; 

這是字符類,我刪除無關代碼(XML序列化屬性等)

public class Character : INotifyPropertyChanged 
{ 
    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      this.NotifyPropertyChanged("Name"); 
     } 
    } 

    private string _player; 
    public string Player 
    { 
     get { return _player; } 
     set 
     { 
      _player = value; 
      this.NotifyPropertyChanged("Player"); 
     } 
    } 

    private string _race; 
    public string Race 
    { 
     get { return _race; } 
     set 
     { 
      _race = value; 
      this.NotifyPropertyChanged("Race"); 
     } 
    } 

    private Race _realRace; 
    public Race RealRace 
    { 
     get { return _realRace; } 
     set 
     { 
      _realRace = value; 
      Race = value.Id; 
      this.NotifyPropertyChanged("RealRace"); 
     } 
    } 

    private string _gender; 
    public string Gender 
    { 
     get { return _gender; } 
     set 
     { 
      _gender = value; 
      this.NotifyPropertyChanged("Gender"); 
     } 
    } 

    private Attributes _attributes; 
    public Attributes Attributes 
    { 
     get { return _attributes; } 
     set 
     { 
      _attributes = value; 
      this.NotifyPropertyChanged("Attributes"); 
     } 
    } 

    private string _class; 
    public string Class 
    { 
     get { return _class; } 
     set 
     { 
      _class = value; 
      this.NotifyPropertyChanged("Class"); 
     } 
    } 

    private Class _realClass; 
    public Class RealClass 
    { 
     get { return _realClass; } 
     set 
     { 
      _realClass = value; 
      Class = value.Id; 
      this.NotifyPropertyChanged("RealClass"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

爲了簡單起見,我一直在測試的屬性是'屬性'p roperty,這是它的代碼:

public class Attributes : INotifyPropertyChanged 
{ 
    private int _accurate; 
    public int Accurate 
    { 
     get { return _accurate; } 
     set 
     { 
      _accurate = value; 
      this.NotifyPropertyChanged("Accurate"); 
     } 
    } 

    private int _cunning; 
    public int Cunning 
    { 
     get { return _cunning; } 
     set 
     { 
      _cunning = value; 
      this.NotifyPropertyChanged("Cunning"); 
     } 
    } 

    private int _discreet; 
    public int Discreet 
    { 
     get { return _discreet; } 
     set 
     { 
      _discreet = value; 
      this.NotifyPropertyChanged("Discreet"); 
     } 
    } 

    private int _persuasive; 
    public int Persuasive 
    { 
     get { return _persuasive; } 
     set 
     { 
      _persuasive = value; 
      this.NotifyPropertyChanged("Persuasive"); 
     } 
    } 

    private int _quick; 
    public int Quick 
    { 
     get { return _quick; } 
     set 
     { 
      _quick = value; 
      this.NotifyPropertyChanged("Quick"); 
     } 
    } 

    private int _resolute; 
    public int Resolute 
    { 
     get { return _resolute; } 
     set 
     { 
      _resolute = value; 
      this.NotifyPropertyChanged("Resolute"); 
     } 
    } 

    private int _strong; 
    public int Strong 
    { 
     get { return _strong; } 
     set 
     { 
      _strong = value; 
      this.NotifyPropertyChanged("Strong"); 
     } 
    } 

    private int _vigilant; 
    public int Vigilant 
    { 
     get { return _vigilant; } 
     set 
     { 
      _vigilant = value; 
      this.NotifyPropertyChanged("Vigilant"); 
     } 
    } 

    private int _toughness; 
    public int Toughness 
    { 
     get { return _toughness; } 
     set 
     { 
      _toughness = value; 
      this.NotifyPropertyChanged("Toughness"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

我想在當在列表框的字符被選擇字段以顯示每個單獨的屬性,此正常工作與直接在字符類的特性,但由於對嵌套屬性和數據綁定的限制,我一直無法使它與'屬性'屬性值一起工作。

XAML的屬性輸入字段中的一個:

<TextBox x:Name="attr_Accurate" DataContext="{Binding Path=(local:Character.Attributes), XPath=SelectedItem, ElementName=listCharacters, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Text="{Binding Path=(local:Accurate)}" PreviewTextInput="NumericInput"/> 

的UpdateSourceTrigger很簡單,就是隻允許整數是在外地輸入的方法:

private void NumericInput(object sender, TextCompositionEventArgs e) 
{ 
    if (!char.IsDigit(e.Text, e.Text.Length - 1)) 
    { 
     e.Handled = true; 
    } 
} 

如果有人可以幫助我所選字符屬性中的值通過數據綁定顯示,我將不勝感激。

+0

你能提供完整的樣本? –

+0

@AyyappanSubramanian我不得不問客戶,你想要完整的解決方案嗎? –

回答

1

使用如下結合:

要顯示Accurate屬性值:

<TextBox x:Name="attr_Accurate" Text="{Binding Path=SelectedItem.Attributes.Accurate), ElementName=listCharacters, Mode=OneWay}" PreviewTextInput="NumericInput"/> 

要顯示Cunning屬性值:

<TextBox x:Name="attr_Accurate" Text="{Binding Path=SelectedItem.Attributes.Cunning), ElementName=listCharacters, Mode=OneWay}" PreviewTextInput="NumericInput"/> 

等一個。

(我不知道,如果你想結合是雙向或一個所以請 變化的需要)

+2

謝謝!這工作完美無缺,並減少了代碼混亂:D,雖然你的代碼有一個'''應該被刪除:) –

+0

@ DionS.Jensen你的歡迎:) –