locked
Why is my binding not working? RRS feed

  • Question

  • My binding is not working and I can not figure out why. The data is there cause if i remove the commented code it work as intended. I guess i am missing some kind of reference?

    XAML
    <UserControl x:Class="Cthulhu_Keeper.Views.BulletElementView"
                 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:Cthulhu_Keeper.Views"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800"
                 DataContext="{Binding RelativeSource={RelativeSource Self}}">
        <Grid>
            <BulletDecorator>
                <BulletDecorator.Bullet>
                    <Ellipse Height="8"
                             Width="8"
                             Fill="Black" />
                </BulletDecorator.Bullet>
                <TextBlock x:Name="ElementText"
                           Text="{Binding Element.Text}"
                           FontSize="{Binding Element.FontSize}"
                           Margin="5,0,0,0"
                           TextWrapping="Wrap"/>
            </BulletDecorator>
        </Grid>
    </UserControl>
    Code
    using System.Windows.Controls;
    
    namespace Cthulhu_Keeper.Views
    {
        public partial class BulletElementView : UserControl
        {
            Models.ArticleElement Element { get; set; }
            public BulletElementView(Models.ArticleElement element)
            {
                Element = element;
                InitializeComponent();
                //ElementText.Text = Element.Text;
                //ElementText.FontSize = Element.FontSize;
            }
        }
    }

    Sunday, June 7, 2020 7:39 AM

All replies

  • Try this:

       public Models.ArticleElement Element { get; set; }

       . . .

       Element = element;

       DataContext = this;

    or this:

       DataContext = element;

       . . .

       Text="{Binding Text}"

    Sunday, June 7, 2020 11:21 AM
  • Thank you, but I am afraid that none of your suggestions made any difference.
    Sunday, June 7, 2020 5:30 PM
  • Hi,

    Thanks for posting here.

    This forum is for discussing the Windows Desktop Development,

    For wpf issue, you could ask here:

    https://docs.microsoft.com/en-us/answers/topics/wpf.html

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 8, 2020 5:27 AM