0

This is maybe a very simple question, still I don't know how to solve this.

I have a model in my MVVM application which I want to bind to a tree view. However I can only bind the tree view to a list of items (or in my case an ObservableCollection). This is the model I currently use:

/// <summary>
/// Represents an group a character can belong to.
/// </summary>
public class OrganisationBase : ModelBase<OrganisationBase>
{
    /// <summary>
    /// The name.
    /// </summary>
    private string name;

    /// <summary>
    /// The parent organization.
    /// </summary>
    private ObservableCollection<OrganisationBase> parentOrganizations;

    /// <summary>
    /// Gets or sets the name of the organization.
    /// </summary>
    public string Name
    {
        get
        {
            return this.name;
        }

        set
        {
            this.name = value;
            this.NotifyPropertyChanged(p => p.Name);
        }
    }

    /// <summary>
    /// Gets or sets the parent organization.
    /// </summary>
    public ObservableCollection<OrganisationBase> ParentOrganizations
    {
        get
        {
            return this.parentOrganizations;
        }

        set
        {
            this.parentOrganizations = value;
            this.NotifyPropertyChanged(p => p.ParentOrganizations);
        }
    }
}

Now my question is: How can I bind this model to my tree view without using Observable collections. I want to do this, because there is no need for an ObservableCollection since every Organisation can only have one parent.

Or, how can I bind the following code to my tree view:

/// <summary>
/// Represents an group a character can belong to.
/// </summary>
public class OrganisationBase : ModelBase<OrganisationBase>
{
    /// <summary>
    /// The name.
    /// </summary>
    private string name;

    /// <summary>
    /// The parent organization.
    /// </summary>
    private OrganisationBase parentOrganizations;

    /// <summary>
    /// Gets or sets the name of the organization.
    /// </summary>
    public string Name
    {
        get
        {
            return this.name;
        }

        set
        {
            this.name = value;
            this.NotifyPropertyChanged(p => p.Name);
        }
    }

    /// <summary>
    /// Gets or sets the parent organization.
    /// </summary>
    public OrganisationBase ParentOrganizations
    {
        get
        {
            return this.parentOrganizations;
        }

        set
        {
            this.parentOrganizations = value;
            this.NotifyPropertyChanged(p => p.ParentOrganizations);
        }
    }
}

This is the code of the tree view I currently use:

<TreeView ItemsSource="{Binding Path=Character.CharacterAllegiances.MemberOf}">
  <TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="IsExpanded"
              Value="True" />
    </Style>
  </TreeView.ItemContainerStyle>
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding ParentOrganizations}">
      <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

Note: The Organisation property is part of a character model. Still when I use ObservableCollections everything displays fine. However as asked above I want to discard this ObservableCollections.

Greetings Ruhrpottpatriot


How to add substitution tags to the subject of the email in SendGrid using the xsmtp-api with the php library

I am trying to set up the name of my client in the subject of the email. This is very crucial for my application, and from what I've read in the SendGrid API docs it is quite possible!.

Info - Substitution tags will work in the Subject line as well as the body of the email.

The problem is that I dob't seem to manage to accomplish this. At first I though that perhaps it's because I am already using the %name% sub within the body of the email, so I've created a new substitution parameter name %nameSubject%, and yet, it won't work.

I use the following code, and the rest of the parameters within the email are workigng just fine:

    /**
    @description Wrapper method in order to handle possible exception due to programmer\ maintainer errors.
    A part of a small wrapper class that I have designed to allow 1-line comfortable use of the the SendGrid and transport classes.
   @returns int - 1, unless there is an internal error, or an exception thrown
    */
public function execute(){
    if(!class_exists('SendGrid') || !class_exists('SendGrid\Mail')){
        throw new exception('Dependency exception SendGrid or SendGrid\Mail are not included');
    }
    if(!isset($this->mailingList) && empty($this->mailingList) || sizeof($this->mailingList) == 0){
        throw new exception('Cannot send an email to an empty set of addresses');
    }
    if(!isset($this->subject, $this->body) || empty($this->subject) || empty($this->body)){
        throw new exception('Cannot send an email without both a subject and a body');
    }

    $sendgrid = new SendGrid(SENDGRID_USER, SENDGRID_PASSWORD);

    // $this->importMailList();
    foreach($this->mailingList as $key => $val) {   
        $this->mail->addTo($key, $val);
    }
    $this->mail->
    setFrom($this->fromMail)-> 
    setSubject($this->subject)->
    setText($this->text)->
    setHtml($this->body);
    if(preg_match('/%name%/', $this->body) && !array_filter($this->mailingList, 'is_int') ){
        $this->mail->addSubstitution("%name%", array_values($this->mailingList));
    }
    return $sendgrid->smtp->send($this->mail);

}

Any help is much appriciated!.

4

2 回答 2

0

TreeView 需要一个项目集合来构建它的节点,这显然是正确的方法并且非常不言自明。

将树视图绑定到单个对象仅在该对象将其自身公开为集合时才有效,例如实现IEnumerable树视图将使用IEnumerable实现来填充数据的方式。

或者,您可以只绑定到针对对象的集合,例如

<TreeView ItemsSource="{Binding object.SomeCollection}" />
于 2013-04-21T14:56:00.090 回答
0

您需要使用 HierarchicalDataTemplate。

将文件夹对象绑定到树视图 将文件对象绑定到树视图

绑定数据(如上面的文件夹和文件),可以参考这篇文章: WPF Treeview: data binding of heterotypes data using CompositeCollection class

于 2013-04-21T15:08:35.680 回答