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



Question:

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


Solution:1

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »