开发者

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

开发者 https://www.devze.com 2022-12-20 18:46 出处:网络
Upda开发者_如何学JAVAte: I\'ve corrected the post, so the question is closed. Expected result: Menu width must be equal to the main window width.

Upda开发者_如何学JAVAte: 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号