Change Header pripiedad in "Expand" control

0

I try to change the property Header of an Expander to vary the title of this variable depending on but there is no way ...

If I add a constant to the Header, it works correctly;

<Expander Name="Header" Margin="4" Header="Config nivel 0"/>

But at the moment I perform a Binding on a variable in the part of the C # of ja to work and the field comes out empty ...

<Expander Name="Header" Margin="4" Header="{Binding Path=header}"/>

And here the part of the C # where I do the binding ...

    public partial class ConfigNivel : UserControl
        {
           int number;
           String header = "Configuración nivel ";


           public ConfigNivel(int number)
           {
               this.number = number;
               header += Convert.ToString(number);
               InitializeComponent();
           }
        }

With the resulting result ...

    
asked by Edulon 08.01.2018 в 21:35
source

1 answer

1

when the data link (binding) is established, the property with the same name in the datacontext is searched I do not see that you have assigned the datacontext,

With a more or less serious mvvm pattern like this:

<Expander Header="{Binding}" Width="200">
<Expander.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Title}" />
        </StackPanel>
    </DataTemplate>
</Expander.HeaderTemplate>
<StackPanel>
    <TextBlock Text="{Binding ContentLine1}" />
    <TextBlock Text="{Binding ContentLine2}" />
    <TextBlock Text="{Binding ContentLine3}" />
</StackPanel>

public class DemoViewModel
{
public string Title { get; set; }
public string ContentLine1 { get; set; }
public string ContentLine2 { get; set; }
public string ContentLine3 { get; set; }
}

public partial class MainWindow : Window
{
public DemoViewModel ViewModel { get; set; }

public MainWindow()
{
    InitializeComponent();
    InitializeViewModel();
}

private void InitializeViewModel()
{
    ViewModel = new DemoViewModel
    {
        Title = "Expander Title",
        ContentLine1 = "This is line 1",
        ContentLine2 = "This is line 2",
        ContentLine3 = "This is line 3"
    };
    this.DataContext = ViewModel;
}
}
    
answered by 08.01.2018 в 22:36