代码后置就是用cs文件实现XAML中的代码。
用(一)中的ListView控件为例:
<ListView Grid.Row="1" x:Name="listView1" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" KeyDown="listView1_KeyDown" >
<ListView.ContextMenu>
<ContextMenu Name="ListMenu1">
<MenuItem Name="copy" Header="{DynamicResource CtrlC_CtrlV.Resource.Copy}" Click="Menu_Cut"/>
<MenuItem Name="english" Header="Change into Chinese" Click="MenuItem_Click" Visibility="Collapsed"/>
<MenuItem Name="chinese" Header="转换成英文" Click="MenuItem_Click"/>
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="{DynamicResource CtrlC_CtrlV.Resource.Value}" Width="50" DisplayMemberBinding="{Binding Path=Value}"/>
<GridViewColumn Header="{DynamicResource CtrlC_CtrlV.Resource.Name}" Width="75" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="{DynamicResource CtrlC_CtrlV.Resource.Detail}" Width="125" DisplayMemberBinding="{Binding Path=Detail}"/>
</GridView>
</ListView.View>
</ListView>
在代码后置中:
ListView listview1 = new ListView();
listview1.HorizontalAlignment = HorizontalAlignment.Left;
listview1.Margin = new Thickness(0, 0, 0, 0);
listview1.VerticalAlignment = VerticalAlignment.Top;
listview1.RenderTransformOrigin = new Point(0.5, 0.5);
listview1.KeyDown += new KeyEventHandler(listView1_KeyDown);
ContextMenu listMenu1 = new ContextMenu();
MenuItem copy = new MenuItem();
copy.Header = "{DynamicResource CtrlC_CtrlV.Resource.Copy}";
copy.Click += new RoutedEventHandler(Menu_Cut);
listMenu1.Items.Add(copy);
//同样的english和chinese
这样据说是可以更灵活。
今天的文章WPFListView控件(三)代码后置分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/72859.html