删除ListView(ListBox)中Binding的item

使用EF框架并在在ListView或ListBox中binding了数据源,每一行数据都对应一个删除按钮,点击这个按钮就删除对应的那条数据。

看似简单的功能也能玩出新花样,下面提供两种方案:

【方案一】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<ListBox FontSize="20" SelectionMode="Single" ItemsSource="{Binding XX}" >
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Width="25" DockPanel.Dock="Right" Height="25" Style="{x:Null}" Tag="{Binding}" Background="{x:Null}" BorderBrush="{x:Null}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Del" TargetObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<TextBlock x:Name="txtContent" DockPanel.Dock="Left" TextAlignment="Center" Text="{Binding xx,Mode=TwoWay}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
1
2
3
4
5
6
7
8
9
10
11
12
13
public void Del(object sender, RoutedEventArgs e)
{
if (sender != null && sender is Button)
{
if (((Button)sender).Tag != null && ((Button)sender).Tag is XX)
{
if (Utils.ShowMessage(this, "确定要删除吗?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
DelOper();
}
}
}
}
阅读更多

VS2012/2013无身份验证远程调试

在VS2012之前的版本中,如果要使用remote debug需要注意一下几种情况:

  • 不能使用“无身份验证”模式
  • 如果不是域环境,本地计算机和远程计算机必须使用相同的系统登陆用户名和密码
  • 远程计算机调试使用的程序需要包含对应的“.pdb”文件

这些条件使得VS远程调试变得非常麻烦,但是从VS2012开始,远程调试功能有了改善,下面介绍一下VS2012或VS2013使用“无身份验证”模式进行远程调试的方法。(以VS2013为例)

1.进入VS安装目录,按如下路径找到Remote Debugger文件夹

\Microsoft Visual Studio 12.0\Common7\IDE\Remote Debugger

阅读更多

浅析StaticResource和DynamicResource

初学WPF的新人有时都会对StaticResource和DynamicResource的概念及应用朦朦胧胧,这里通过一个小例子彻底弄懂这两者的区别及用法。

首先是前台XAML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<Window x:Class="WpfApplication50.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="Brush" Color="Red" />
</Window.Resources>
<StackPanel>
<Button Margin="5" Content="Change Resource" Click="Button_Click" />
<Label x:Name="lbl1" Margin="5" Content="Static Resource(Window.Resources)" Background="{StaticResource Brush}" />
<Label x:Name="lbl2" Margin="5" Content="Static Resource(Label.Resources)" Background="{StaticResource Brush}" >
<Label.Resources>
<SolidColorBrush x:Key="Brush" Color="Blue" />
</Label.Resources>
</Label>
<Label x:Name="lbl3" Margin="5" Content="DynamicResource(Window.Resources)" Background="{DynamicResource Brush}" />
<Label x:Name="lbl4" Margin="5" Content="DynamicResource(Label.Resources)" Background="{DynamicResource Brush}" >
<Label.Resources>
<SolidColorBrush x:Key="Brush" Color="Blue" />
</Label.Resources>
</Label>

</StackPanel>

</Window>

后台的代码:

1
2
3
4
5
private void Button_Click(object sender, RoutedEventArgs e)
{
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
this.Resources["Brush"] = brush;
}
阅读更多

WPF隐藏ListView中GridView的列

有需求要根据某变量的值将ListView中GridView的某一个GridViewColumn做隐藏设置,经过一番思考,总结了两种方法,但都需要对ListView中的GridView以及每一个GridViewColumn做命名:

1
2
3
4
5
6
7
8
<ListView x:Name="lvTest"  SelectionMode="Single">
<ListView.View>
<GridView x:Name="gvTest" >
<GridViewColumn x:Name="gvcName" Width="85" />
<GridViewColumn x:Name="gvcAge" Width="85" />
</GridView>
</ListView.View>
</ListView>

方法一:设置列宽

这种方法相对简单,如果没有特殊条件限制,可以直接设置某一列的width=0,如:

gvcName.Width = 0

阅读更多

WPF获取ContextMenu的源

在WPF中若使用多个控件,并给这些控件添加同一个ContextMenu,那么如何获取ContextMenu的鼠标点击源?

WPF的ContextMenu并没有Winform的ContextMenu的SourceControl这一属性,但我们也可以通过另一种方法实现同样的功能。

在ContextMenu的MenuItem的点击事件中使用ContextMenu.PlacementTarget

1
2
3
4
5
public void MenuItem_Click(object sender, RoutedEventArgs e)
{
ContextMenuService.GetPlacementTarget(
LogicalTreeHelper.GetParent(sender as MenuItem));
}

EF一对多关系删除问题

在使用EF删除一对多关系的实体时,碰到如下提示:

“其他信息: 操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null。对关系作出更改后,会将相关的外键属性设置为 null 值。如果外键不支持 null 值,则必须定义新的关系,必须向外键属性分配另一个非 null 值,或必须删除无关的对象。”

原因很简单,假设一个A对应多个B,那么B中的外键就是A的主键,如果先删除A,接着又把B读取到datacontext中

var lstB = a.B.ToList();

container.A.DeleteObject(a);

就会导致B中的外键失去关联,所以便产生了错误,解决的办法有下面几种:

阅读更多

125 Basic C# Interview Questions and Answers

  最近把wordpress迁到github,但发现之前的域名已经过期了,但是wordpress的后台还是绑定了这个域名,导致没办法登陆后台。后来发现修改数据库即可解决,方法如下:

  1. 用phpmyadmin登录数据库
  2. 找到“wp_options”表
  3. 修改“siteurl”字段为新的域名

125 Basic C# Interview Questions and Answers

英文原文:http://theprofessionalspoint.blogspot.jp/2013/10/125-basic-c-interview-questions-and.html

Below is the list of 125 basic C# interview questions with their answers. These C# interview questions and?answers?are very simple and straight-forward which cover the basic concepts of C# mostly related to object oriented concepts. So if you are preparing for C# interview, I will suggest you to must go through these 125 C# basic interview questions and answers to revise your C# concepts. Here goes the list of 125 basic C# interview questions and answers.

1. What is C#?

C# (pronounced “C sharp”) is a simple, modern, object-oriented, and type-safe programming language. It will immediately be familiar?to C and C++ programmers. C# combines the high productivity of Rapid Application Development (RAD) languages.

2. What are the types of comment in C#?

阅读更多

DataGrid绑定Dictionary问题

【问题】

在最近的项目中使用DataGrid的DataGridCheckBoxColumn绑定了后台TagModel类的IsSelected字段,数据源是TagModel类型的Dictionary,运行发现Checkbox不能正常勾选,提示错误:此视图不允许“EditItem”。

【问题重现】

前台:

1
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" />
阅读更多

WPF之DragDrop拖放实例---图像资源管理器

1. 问题

在winform或wpf开发中,常会用到DragDrop拖放功能,如拖拽一个文件到程序窗体,则在文本控件上显示文件的路径,其他内容控件显示文件内容,这样省去输入文件路径或者打开文件对话框的麻烦。在实际应用中,我们也会看到一些影音播放器支持对拖放文件的播放,Office支持对拖放文件的插入等等。综合上述情况,多数情况下,我们关注了从程序外部拖放文件至程序,也就是拖过来。

那么如果现在做一个功能,需要我们把程序中的内容拖放到另一个程序中,比如直接拖拽程序中的图片控件到Word,就把图片控件中的图粘贴到Word中,也就是拖过去,这要怎么实现?

阅读更多