Hi All,
I am developing a tool for Autodesk Revit using the pyrevit extension and IronPython. For this, I have implemented a custom control called IntegerUpDown, which I need to use in my tool. I tested the control in Visual Studio, and it works well. However, I am struggling to use my control in pyrevit by referencing it's name space in my main xaml file, considering the structure of my project attached below:
Please check my references:
IntegerUpDown.xaml :
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="50">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<TextBox x:Name="NumericText" Grid.Column="0"
Text="0"
TextAlignment="Center"
BorderBrush="Black" Margin="0,0,0.2,0" />
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<RepeatButton x:Name="PART_IncreaseButton" Click="btnIncrease_Click" Grid.Row="0" Margin="0,0,0,0.1"
Width="15" BorderThickness="0.75">
<Polygon x:Name="PART_Up_Polygon"
HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="2"
StrokeThickness="0.5" Stroke="Transparent"
Points="0,0 -2,5 2,5" Stretch="Fill" Fill="Black"/>
</RepeatButton>
<RepeatButton x:Name="PART_DecreaseButton" Click="btndecrease_Click" Grid.Row="1" Margin="0,0.1,0,0"
Width="15" BorderThickness="0.75">
<Polygon x:Name="PART_Down_Polygon" HorizontalAlignment="Center"
VerticalAlignment="Center" Margin="2"
StrokeThickness="0.5" Stroke="Transparent"
Points="-2,0 2,0 0,5 " Stretch="Fill" Fill="Black"/>
</RepeatButton>
</Grid>
</Grid>
</UserControl>
IntegerUpDown.py:
import os, clr, wpf
from System.Windows.Controls import UserControl
script_path = os.path.dirname(__file__)
class IntegerUpDown(UserControl):
def __init__(self):
xaml_path = os.path.join(script_path, 'IntegerUpDown.xaml')
wpf.LoadComponent(self, xaml_path)
def btnIncrease_Click(self, sender, e):
num = int(self.NumericText.Text)
num += 1
self.NumericText.Text = str(num)
def btnDecrease_Click(self, sender, e):
num = int(self.NumericText.Text)
num = max(num - 1, 0)
self.NumericText.Text = str(num)
grids.xaml (Main xaml):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!--I am struggling what putting here?-->
xmlns:local="clr-namespace:...."
Title="Grids"
Height="500" Width="340"
WindowStartupLocation="CenterScreen">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="86*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<DockPanel >
<Label Content="Nom :" />
<TextBlock Text="Grid for Revit" Width="150" Margin="0,4,0,0" />
</DockPanel>
<TabControl Grid.Row="1">
<TabItem Header="X" Width="50">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65*"/>
<ColumnDefinition Width="35*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="7*"/>
<RowDefinition Height="79*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<DockPanel Grid.ColumnSpan="2">
<Label Content="Position:" Margin="0,0,40,0"/>
<Label Content="Repeter x:" Margin="0,0,40,0"/>
<Label Content="Espacement:"/>
</DockPanel >
<DockPanel Grid.Row="1" Grid.ColumnSpan="2">
<TextBox Width="50" Text="0.00" Margin="0,0,5,0"/>
<TextBlock Text="(m)" Margin="0,0,25,0" Width="20"/>
<IntegerUpDown x:Name="MyUpDown" Minimum="00" Margin="0,0,50,0"/>
<TextBox Width="50" Text="0.00" Margin="0,0,5,0"/>
<TextBlock Text="(m)" Width="20" HorizontalAlignment="Left" />
</DockPanel>
<ListBox Grid.Row="2" Margin="0,5,0,0">
<ListBoxItem>
1
</ListBoxItem>
<ListBoxItem>
2
</ListBoxItem>
<ListBoxItem>
3
</ListBoxItem>
</ListBox>
<StackPanel Grid.Column="1" Grid.Row="2">
<Button Name="ajouter_X" Content="Ajouter" Click="ajouter_X_Click" VerticalAlignment="Top" Margin="5,50,5,10"/>
<Button Name="supprimer_X" Content="Supprimer" Click="supprimer_X_Click" Margin="5,0,5,10"/>
<Button Name="supprimer_tout_X" Content="Supprimer tout" Click="supprimer_tout_X_Click" Margin="5,0,5,10"/>
</StackPanel>
</Grid>
</TabItem>
</TabControl>
<DockPanel Grid.Row="2">
<Button Content="Appliquer" Width="100" Height="25"/>
<Button Content="Fermer" Width="100" Height="25" Click="Fermer_Click" HorizontalAlignment="Right"/>
</DockPanel>
</Grid>
</Window>
Grids_script.py (Main script) :
# -*- coding: UTF-8 -*-
import wpf, clr, os
from System.Windows import Window
script_path = os.path.dirname(__file__)
class Mywindow(Window):
def __init__(self):
xaml_path = os.path.join(script_path, 'grids.xaml')
wpf.LoadComponent(self, xaml_path)
def ajouter_X_Click(self, sender, e):
pass
def supprimer_X_Click(self, sender, e):
pass
def supprimer_tout_X_Click(self, sender, e):
pass
def Fermer_Click(self, sender, e):
self.Close()
if __name__ == "__main__":
Grids = Mywindow()
Grids.ShowDialog()
Any help would be appreciated
Thanks.