Dmitry Dyachkov
Dmitry Dyachkov

Reputation: 1785

Property Binding to Static Properties in WPF: Width binding doesn't work

Update: I've corrected the post, so the question is closed.

Expected result: Menu width must be equal to the main window width. Here is full code:

<Menu Name="menu1" DockPanel.Dock="Top">
   <Menu.Width>
      <Binding Path="MainWindow.Width" 
               Source="{x:Static Application.Current}"
               UpdateSourceTrigger="PropertyChanged"/>
   </Menu.Width>
   <MenuItem Header="File">
       <MenuItem Header="Open" />
       <MenuItem Header="Close" />
   </MenuItem>
</Menu>

The result: Menu width is equal to content width

Upvotes: 0

Views: 1108

Answers (1)

Anvaka
Anvaka

Reputation: 15823

Doh,

Dmitry, Application.Current doesn't have a property Width...

Use Grid instead of DockPanel:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <Menu Name="menu1" VerticalAlignment="Top">
         <MenuItem Header="File">
            <MenuItem Header="Open"/>
            <MenuItem Header="Close"/>
         </MenuItem>
      </Menu>
   </Grid>
</Page>

Don't overcomplicate things. And I really encourage you to read wonderful Dr. WPF articles: ItemsControl from A to Z

Cheers, Anvaka

Upvotes: 1

Related Questions