icsharpcode / WpfDesigner

The WPF Designer from SharpDevelop
MIT License
949 stars 251 forks source link

DesignItemBinding example #103

Closed SoftControlDev closed 3 months ago

SoftControlDev commented 1 year ago

@jogibear9988 Hi, do you have any examples of using this? Even a few code snippets or screenshots without explanations would help me get going.

Thank you

jogibear9988 commented 1 year ago

for example, we have in our software own property editorviews for different elements.

in these xaml views we then use designitembinding to bind controls, so they directly modify the elemnts properties in the design item, see:

    <TextBox Grid.Column="1"
                 VerticalAlignment="Center"
                 Text="{DesignItemBinding ObjectName,
                 SingleItemProperty=True,
                 UpdateSourceTrigger=PropertyChanged}" MaxHeight="24" Margin="5,6,14,5" Grid.ColumnSpan="2">
            <i:Interaction.Behaviors>
                <cbtk:TextUpdateBindingBehavior />
            </i:Interaction.Behaviors>
        </TextBox>

in this case, our control has a property named ObjectName

jogibear9988 commented 1 year ago

more:

    <TextBox Grid.Row="2" Grid.Column="2"
                 VerticalAlignment="Center"
                 Text="{DesignItemBinding PlcName,
                                          UpdateSourceTrigger=PropertyChanged, UpdateSourceTriggerMultipleSelected=Explicit}">
            <i:Interaction.Behaviors>
                <cbtk:TextUpdateBindingBehavior />
            </i:Interaction.Behaviors>
        </TextBox>

or

   <xctk:DoubleUpDown Grid.Row="3"
                           Grid.Column="2"
                           VerticalAlignment="Center" Value="{DesignItemBinding FontSize}" ></xctk:DoubleUpDown>

or

    <CheckBox   Grid.Row="13"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    IsChecked="{DesignItemBinding {x:Static a:MultiPageControl.DuplicateElementProperty }}" Grid.Column="3"/>

or

        <CheckBox Margin="0,5,5,5"
                      Content="{Binding ElementName=root,
                      Path=TechnologyScheme.TelegramConfiguration.TelegramNames.RecRes1ShortForm,
                      FallbackValue=R1}"
                      IsChecked="{DesignItemBinding IsTelegramRecRes1Enabled}" 
                      Visibility="{Binding ElementName=root, Path=TechnologyScheme.TelegramConfiguration.TelegramNames.RecRes1ShortForm, Converter={StaticResource TelegramVisibilityConverter}}"/>
SoftControlDev commented 1 year ago

Are these controls actual DesignItem elements in your program? For instance, in your first example is the <TextBox ...> object a DesignItem? If so, that might explain why my attempts at using DesignItemBinding are not working (TargetObject is always null)?

Here's my use case, I created a CustomButton : Button {} . I have a customization screen that simplifies customizing this button (vs. using the PropertyGrid). This screen is shown through a context menu that is accessed by righ-clicking on the CustomButton when it is on the design surface. I created a bunch of additional custom properties on the button. Two of them are Lists and MyCustomClass has properties that need editing.

So I have most of my code working by using DesignItemProperty.SetValue() method. But it has recently become even more complicated now that I am saving a LinearGradientBrush in MyCustomClass. The GradientStops are not getting saved to the xaml, unless I find the list that is currently being modified (since there is two of those lists), find which item it is, then grab the LinearBrush DesignItem and set the GradientStops property on it, using SetValue(). Crazy part is in the design item, the Value, Designer Value, and ValueOnInstance all show the correct GradientStops. It even works properly in the designer/design mode...the issue is the gradientstops aren't saved/commited to the XAML text. (unless I force them)

If this was a one time thing, I'd settle with what I can currently get to work, but I plan on making "a lot" of these simplified customization screens that modify custom properties.

SoftControlDev commented 1 year ago

@jogibear9988 Here are the classes and properties I was talking about, hope this helps paint a clearer pciture

    public class YDesignerButton : Button, IWpfContentCustomizable
    {
        #region Constructors

        static YDesignerButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(YDesignerButton), new FrameworkPropertyMetadata(typeof(YDesignerButton)));
        }

        public YDesignerButton() : base()
        {
            MouseActions = new BindingList<MouseEventAction>();
            LogicalActions = new BindingList<LogicalEventAction>();
        }

        #region ICustomizable Implementation
        private string _identifier;
        public string Identifier
        {
            get { return _identifier; }
            set
            {
                _identifier = value;
                FirePropertyChanged();
            }
        }

        private ContentType _contentType;
        public ContentType ContentType
        {
            get { return _contentType; }
            set
            {
                _contentType = value;
                FirePropertyChanged();
            }
        }

        private object _controlContent;
        public object ControlContent
        {
            get { return _controlContent; }
            set
            {
                _controlContent = value;
                FirePropertyChanged();
            }
        }

        private string _text;
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                FirePropertyChanged();
            }
        }

        private BindingList<MouseEventAction> _mouseActions;
        public BindingList<MouseEventAction> MouseActions
        {
            get { return _mouseActions; }
            set
            {
                _mouseActions = value;
                FirePropertyChanged();
            }
        }

        private BindingList<LogicalEventAction> _logicalActions;
        public BindingList<LogicalEventAction> LogicalActions
        {
            get { return _logicalActions; }
            set
            {
                _logicalActions = value;
                FirePropertyChanged();
            }
        }
}
  public class MouseEventAction : EventActionBase
    {
        public MouseEventAction()
        {

        }

        private MouseActions _mouseAction;
        public MouseActions MouseAction
        {
            get { return _mouseAction; }
            set
            {
                _mouseAction = value;
                FirePropertyChanged();
            }
        }

        public override string ToString()
        {
            return "Mouse Event --> Action";
        }

    }
 public abstract class EventActionBase : ObservableObject
    {

        private object _parameter;
        public object Parameter
        {
            get { return _parameter; }
            set
            {
                _parameter = value;
                FirePropertyChanged();
            }
        }

        private UIActions _uiAction;
        public UIActions UIAction
        {
            get { return _uiAction; }
            set
            {
                _uiAction = value;
                FirePropertyChanged();
            }
        }

        public override string ToString()
        {
            return "Event --> Action";
        }
    }
SoftControlDev commented 1 year ago

I use data templates on ItemsControl for the BindingList \<MouseEventAction> , and when the UIAction (my enum) is set to UIAction.ChangeBackgroundColor ... then I display a brush editor that is using a Binding to the Parameter object on the EventActionBase class. When the object is is a LinearGradientBrush, the only way I can get the GradientStops to commit to xaml is to "hunt" that property down manually in the code. Which I hate doing.

jogibear9988 commented 1 year ago

I don't understand what you trying to do. Maybe you can upload a complete sample?

SoftControlDev commented 1 year ago

would you mind doing a quick GoToMeeting? I can set it up. I won't take more than 10min of your time.