Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Data Binding in Custom Control
    primarykey
    data
    text
    <p>I'm trying to create a custom TabItem that is dynamically added to a TabControl defined in a WPF Window. My custom control has an object that contains data I want to bind to specific parts of the template.</p> <pre><code>&lt;Style TargetType="{x:Type local:EntityTabItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:EntityTabItem}"&gt; &lt;Border&gt; &lt;Grid&gt; &lt;Border x:Name="borderTop" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"/&gt; &lt;StackPanel Orientation="Horizontal" Margin="0,0,2,0"&gt; &lt;!-- Want to bind the FileName to this TextBlock --&gt; &lt;TextBlock VerticalAlignment="Center" Text="{Binding Path=Entity.FileName}" Margin="-1,0,0,0" Padding="6,1,10,1"/&gt; &lt;Button x:Name="closeButton" VerticalAlignment="Center" Content="X" Style="{StaticResource TabCloseButton}"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="ContentTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;Grid Background="White"&gt; &lt;!-- Want to bind the FileText to this TextBox --&gt; &lt;TextBox Margin="15,0,0,0" BorderBrush="{x:Null}" Text="{Binding Path=Entity.FileText}"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>This is the custom control CS file:</p> <pre><code>public class EntityTabItem : TabItem { public Entity MyEntity { get; set; } public EntityTabItem(string path) { this.MyEntity = new Entity(path); } static EntityTabItem() { DefaultStyleKeyProperty.OverrideMetadata(typeof(EntityTabItem), new FrameworkPropertyMetadata(typeof(EntityTabItem))); } } </code></pre> <p>I'm fairly certain I need to setup a DataBinding/Source somewhere, but I can't figure out where to bind it for the bindings in my TextBlock to work.</p> <p>I honestly cannot wrap my head around DataBinding at all. Half the time, I get it to work just fine without realizing how, and the other half of the time it doesn't do anything.</p> <p>I tried also implementing the "Entity" object as a DependencyProperty, but could not get that to work either. As I'm only creating my custom TabItem in CS (never used directly in XAML), does this even matter?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload