Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Problem about Revit DockablePane development ,Using the MVVM pattern,but the Binding does not work .

1755384511
Participant

Problem about Revit DockablePane development ,Using the MVVM pattern,but the Binding does not work .

1755384511
Participant
Participant

I established a project which contain Revit DockPanel, then I can't change my properties in the ViewModel using the MVVM pattern. 

the mvvm framework is provided by Prism,and the dll can be loaded by Revit; HERE is the GUI:

1755384511_1-1715840823750.png

if I push the [TEST] button , the label "02"  should become string-value "CLICKED", BUT it's dose not work ;

AND the property [Message] should be the string-value "INIT"  at begining , But it dose not;

HOWEVER,If I am use the same code with xaml-Control  <Window> , not the <Page>for dockpanel , there is no problem ;

So ,I don't why ,

maybe property not be load to the dockpanel at begining?

maybe some problem happend at setup the dockpanle? 

maybe the mvvm can't be used in the xaml<Page> ?

What the problem on earth?  

//================================

here is the code: 

TemplateCommand.cs

 

namespace SEPD.Drawing.DockPanel.Demo
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    [Journaling(JournalingMode.UsingCommandData)]
    public class TemplateCommand : IExternalCommand
    { 
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication application = commandData.Application;
    
            TemplateView templateView = new TemplateView();
            templateView.DataContext = new TemplateViewModel( ); 
  
            var guid = new Guid("{ED7FB4B6-572A-43B9-A3AB-DCAA251531B7}");
            DockablePaneId paneId = new DockablePaneId(guid);
            DockablePane pane = application.GetDockablePane(paneId);
            pane.Show();
 
            return Result.Succeeded;
        } 
    }
}

 

 TemplateView.xaml

 

<Page x:Class="SEPD.Drawing.DockPanel.Demo.TemplateView"
             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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:local="clr-namespace:SEPD.Drawing.DockPanel.Demo"
             mc:Ignorable="d" 
             Title="TITLE"
             Height="450" Width="500"
             d:DesignHeight="450" d:DesignWidth="500">

    <Page.Resources>
        <ResourceDictionary>

            <local:IntBoolConvert x:Key="intBoolConvert"/>
            <local:PercentageConverter x:Key="percentageConverter"/>

            <Style x:Key="TextBoxStyleBase" TargetType="{x:Type TextBox}">
                <Setter Property="TextWrapping" Value="Wrap"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Width" Value="150"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="Margin" Value="2"/>
            </Style>

            <Style x:Key="ButtonStyleBase" TargetType="{x:Type Button}">
                <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Margin" Value="2"/>
            </Style>

            <Style x:Key="ComboxStyleBase" TargetType="{x:Type ComboBox}">
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Margin" Value="5"/>
            </Style>

            <Style x:Key="LabelStyleBase" TargetType="{x:Type Label}">
                <Setter Property="Width" Value="60"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Margin" Value="2"/>
            </Style>

        </ResourceDictionary>
    </Page.Resources>

    <TabControl>
        <TabItem Header="002">
            <Grid>
                <StackPanel>
                    <Button Content="TEST" Command="{Binding TempCommand2 }" Style="{StaticResource ButtonStyleBase}"/>
                    <Label Content="{Binding Message,FallbackValue=02}" Style="{StaticResource LabelStyleBase}"/>
                </StackPanel>
            </Grid>
        </TabItem> 
    </TabControl>


</Page>

 

TemplateView.xaml.cs

 

namespace SEPD.Drawing.DockPanel.Demo
{
/// <summary>
/// TemplateView.xaml 的交互逻辑
/// </summary>
public partial class TemplateView : Page, IDockablePaneProvider
{
    public TemplateView()
    {
        InitializeComponent();
    }

    public void SetupDockablePane(DockablePaneProviderData data)
    {
        data.FrameworkElement = this as FrameworkElement;
        data.InitialState = new DockablePaneState();
        data.InitialState.DockPosition = DockPosition.Left;
    }
}
}

 

TemplateViewModel.cs

 

namespace SEPD.Drawing.DockPanel.Demo
{
    public partial class TemplateViewModel : BindableBase
    { 
        // cur_DIR
        static readonly string assemblyPath = Assembly.GetExecutingAssembly().Location;
        static readonly string assemblyDir = Path.GetDirectoryName(assemblyPath) + "\\";
         
        public ICommand TempCommand2 { get; set; } 
        public TemplateViewModel( )
        { 
            PreProccess(); 
            this.TempCommand2 = new DelegateCommand(()=>{
              Message = "CLICKED"; 
             }); 
        } 
        
         //property
          private string _message = "INIT";
          public string Message
         {
            get => _message;
             set => SetProperty(ref _message, value);
         }
    }
}

 

 

 

 

 

0 Likes
Reply
Accepted solutions (1)
235 Views
2 Replies
Replies (2)

ricaun
Advisor
Advisor
Accepted solution

Looks like you are not setting the DataContext inside the DockablePane.

 

This code below is not gonna work. You create a new instance of the Page that is not the Page inside the DockablePane.

 

 

TemplateView templateView = new TemplateView();
templateView.DataContext = new TemplateViewModel(); 

 

 

 

In your case you need to add the DataContext inside the TemplateView constructor, something like this:

 

 

 

public partial class TemplateView : Page, IDockablePaneProvider
{
    public TemplateView()
    {
        InitializeComponent();
        this.DataContext = new TemplateViewModel(); 
    }

    public void SetupDockablePane(DockablePaneProviderData data)
    {
        data.FrameworkElement = this as FrameworkElement;
        data.InitialState = new DockablePaneState();
        data.InitialState.DockPosition = DockPosition.Left;
    }
}

 

 

 

By default, Revit gonna create the his own instance of the TemplateView.


I usually create a static instance of the ViewModel, so I can control in other places of the code.

 

PS: You can add a background color in the Page to remove that black background in the DockablePane.

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes

1755384511
Participant
Participant
Oh~What a stupid mistake I have make~nearly forgot the key of MVVM;
Any way, Success! Thank you very much!
0 Likes