Articles in this section
Category / Section

How to create a Custom Property window and add custom document properties in the Spreadsheet control?

1 min read

Document properties, also known as metadata, are details about a file that describe or identify it. Document properties include details such as title, author name, subject, keywords, etc. You can add custom document properties to the workbook as given in the following code sample.

C#

Spreadsheet.ExcelProperties.WorkBook.CustomDocumentProperties["Name"].Text = “Sample”;  

Adding Custom Document Properties through User Interface, that is, Custom Property Window:

The Spreadsheet control allows you to manually create the Custom Property window, where you can add the custom document properties as done in Excel such as Name, Type and Value. The following code example shows how to create a custom property window in XAML.

XAML

<StackPanel Orientation="Horizontal" Margin="5">
    <StackPanel Orientation="Vertical" Width="350">
        <StackPanel Orientation="Horizontal" Margin="5">
            <TextBlock Text="Name" Width="75" Margin="2" Height="30">
            </TextBlock>
            <TextBox Name="Name_TxtBox" Width="100" Margin="2" Height="30"/>             
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="3">
            <TextBlock Text="Type" Width="75" Margin="2" Height="30" / >
             <ComboBox Name="Type_combo" Width="100" Margin="2" 
                      Height="30" ></ComboBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="3">
            <TextBlock Text="Value" Width="75" Margin="2" Height="30" />
            <TextBox Name="Value_TxtBox" Width="100" Margin="2" Height="30"/ >
          </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="3" >
            <TextBlock Text="Properties" Width="75" Margin="2" Height="30" />
            <DataGrid Height="200" Width="500" Name="propertygrid" 
                      ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">                        
            </DataGrid>
        </StackPanel>
    </StackPanel>         
    <StackPanel Orientation="Vertical" Margin="10,10,0,0">
        <Button x:Name="AddBtn" Content="Add" Click="AddBtn_Click" Width="50" 
                Height="30" Margin="5"/>
    </StackPanel>
</StackPanel>

To create an Observable collection to maintain the Custom properties of the Workbook:

C#

ObservableCollection<CustomProperties> customProperties = new ObservableCollection<CustomProperties>();
 this.DataContext = customProperties;

The properties defined in the window are as follows:

C#

string name;
object value;
string type;
this.Type_combo.ItemsSource = new List<string>() { "Text", "Date", "Number", "Yes or No" };

By clicking the Add button in the Custom Property window, the properties of the Workbook that were entered are stored in the CustomDocumentProperties of the Workbook.

C#

private void AddBtn_Click(object sender, RoutedEventArgs e)
{
    switch(this.Type_combo.SelectedValue.ToString())
    {
        case "Text":
            this.Workbook.CustomDocumentProperties[this.Name_TxtBox.Text].Text =                                                                                                             this.Value_TxtBox.Text;
            break;
case "Date":
            this.Workbook.CustomDocumentProperties[this.Name_TxtBox.Text].DateTime =                                                                            Convert.ToDateTime(this.Value_TxtBox.Text);
            break;
        case "Number":
            this.Workbook.CustomDocumentProperties[this.Name_TxtBox.Text].Double =                                                                           Convert.ToDouble(this.Value_TxtBox.Text);
            break;
        case "Yes or No":
            this.Workbook.CustomDocumentProperties[this.Name_TxtBox.Text].Boolean =                                                                         Convert.ToBoolean(this.Value_TxtBox.Text);
            break;                   
    }
    customProperties.Add(new CustomProperties { Name = this.Name_TxtBox.Text, Value =  
       this.Value_TxtBox.Text, Type = this.Type_combo.SelectedItem.ToString() });
    propertygrid.ItemsSource = customProperties;
}  

The following image is a screenshot of the Custom Property window.

Figure 1: Custom Property window

You can also add the custom property by entering the property’s name, type, and value in the text boxes and clicking the Add button in the Property window.

Sample Link:

http://www.syncfusion.com/downloads/support/directtrac/general/CustomPropertyUpdated-1951247821.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied