WPF Tips

1. About the binding expression "{Binding Path=.}" in XAML, here is one example of TreeView Control:

We can give its ItemsSource property a binding of "{Binding Path=.}", which is a way of indicating that the ItemsSource property is bound to the TreeView's DataContext.

2. One tip to use the <Run/> element in WPF

<StackPanel>

<TextBlock>

<Run FontWeight="Bold">Publisher: </Run>

<TextBlock Text="{Binding Path=Publisher}" TextWrapping="Wrap"></TextBlock> 

</TextBlock>

<TextBlock> 

<Run FontWeight="Bold">Version: </Run> 

<TextBlock Text="{Binding Path=Version}"></TextBlock> 

</TextBlock>

</StackPanel>

For such simple usage, you don't have to use the converter, just use the <Run> element to solve this issue.

 3. The DataGrid or ComboBox's SelectedItem set to null or TabControl switch:

For the SelectedItem we always use OneWayToSource binding, this is why that issues happens, seems the real reason is visual tree related. Here is one soulution, just use the TwoWay binding instead of OneWayToSource, but sometime it doesn't work.

 

4. In order to use MVVM pattern, the initialization operation MainWindow load placed in App.xaml.cs. But there is a strange phenomenon, when you run the program, will load two MainWindow instance, and the second of MainWindow datacontext is empty. By stepping discovered that when finished OnStartUp (App.xaml.cs in) method, with a re-run of the MainWindow constructor, due here just calls the InitializeComponent, no assignment for DataContex, so second loading window no bound data.

This phenomenon is due to the default of the entire application is running for the first time the page is loaded MainWindow window, you need to App.xaml, the StartupUri property of the Application node deleted, you will find this property The default value is "MainWindow. xaml ". After deleted will only initialize an instance of.

Reproduced in: https: //www.cnblogs.com/CSharpSPF/archive/2012/01/14/2322571.html

Guess you like

Origin blog.csdn.net/weixin_34160277/article/details/93530993