3 Replies - 219 Views - Last Post: 10 April 2019 - 03:36 AM Rate Topic: -----

#1 JohnnyOneEye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-January 16

WPF property binding only working on project load

Posted 09 April 2019 - 03:17 PM

Hi im new to WPF and im using it rewrite a ui for an old project. im trying to set properties for a menu but using the code ive found in several places on the net i have come across a problem i cant find a solution for. if i set the custom property when i use MyControl it will display in the designer. when i build the project the value showing is the default value. if i then unload and relode the project, the desired value works. if i build it goes back to default which in this example i have no fallback so its null. can someone please help me understand the issue.

<UserControl x:Class="Test.MyControl"
             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:local="clr-namespace:Test"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             Background="AliceBlue">
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <StackPanel>
                <ContentControl 
                      Content="{Binding Heading ,RelativeSource={RelativeSource AncestorType=local:MyControl}}"/>

            </StackPanel>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>



using System.Windows;
using System.Windows.Controls;

namespace Test{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class MyControl : UserControl{
        public MyControl()=> InitializeComponent();
        
        public string Heading {
            get => (string)GetValue(HeadingProperty);
            set => SetValue(HeadingProperty, value);
        }
        public static DependencyProperty HeadingProperty = DependencyProperty.Register("Heading", typeof(string), typeof(MyControl), new UIPropertyMetadata("Insert Heading"));
        public static DependencyProperty HeadingTemplateProperty = DependencyProperty.Register("HeadingTemplate", typeof(DataTemplate), typeof(MyControl));
    }
}




<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:MyControl Heading="Control Heading 1"/>
        <local:MyControl Heading="Control Heading 2"/>
        
    </StackPanel>
</Window>



Thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: WPF property binding only working on project load

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,493
  • Joined: 05-May 12

Re: WPF property binding only working on project load

Posted 09 April 2019 - 06:56 PM

I can't reproduce your problem. The heading shows up on first run. No need to unload and reload.
Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 JohnnyOneEye   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-January 16

Re: WPF property binding only working on project load

Posted 09 April 2019 - 11:39 PM

Skydiver thankyou for testing that. it could be a problem with my visual studio designer.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,493
  • Joined: 05-May 12

Re: WPF property binding only working on project load

Posted 10 April 2019 - 03:36 AM

Make sure that your antivirus has an exception for your project directory.

Make sure that you are not leaving a stale copy of your program running.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1