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. 为了使用MVVM模式,将MainWindow的初始化加载操作放到了App.xaml.cs中。但出现了一个奇怪的现象,运行程序时,会加载出两个MainWindow实例,并且第二个MainWindow的datacontext为空。经单步调试发现,当OnStartUp(App.xaml.cs中)方法执行完毕之后,用重新执行了MainWindow的构造函数,由于这里只是调用了InitializeComponent,没有为其DataContex赋值,所以第二次加载的窗口没有绑定的数据。

出现这种现象是因为整个应用程序默认的运行时首次加载的页面是MainWindow窗口,需要到App.xaml中,将Application节点中的StartupUri属性删掉,你会发现这个属性默认的值是“MainWindow.xaml"。删掉之后就只会初始化一个实例了。

转载于:https://www.cnblogs.com/CSharpSPF/archive/2012/01/14/2322571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值