我是WPF的新手,我正在使用DataGrids,我需要知道属性ItemsSource何时被更改.
例如,我需要在执行此指令时必须引发事件:
dataGrid.ItemsSource = table.DefaultView;
Run Code Online (Sandbox Code Playgroud)
或者添加行时.
我试过使用这段代码:
CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(myGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);
Run Code Online (Sandbox Code Playgroud)
但此代码仅在用户向集合添加新行时才有效.因此,当整个ItemsSource属性发生任何更改时,我需要引发一个事件,因为整个集合被替换或者因为添加了一行.
我希望你能帮助我.先感谢您
我正在学习LINQ-to-SQL,一切都很顺利,直到发生奇怪的事情:
我试着做一个例子distinct,所以,使用Northwind dabatase我写了以下查询:
var query =
from o in db.Orders
orderby o.CustomerID
select new
{
o.CustomerID
};
Run Code Online (Sandbox Code Playgroud)
如果我打印LINQ-to-SQL为其中存储的查询生成的SQL,query则如下所示:
SELECT [t0].[CustomerID]
FROM [dbo].[Orders] AS [t0]
ORDER BY [t0].[CustomerID]
Run Code Online (Sandbox Code Playgroud)
所以,像往常一样,查询把所有的CustomerID每一个Order中Orders按字母顺序排序表.
但!如果我使用这样的Distinct()方法:
var query = (
from o in db.Orders
orderby o.CustomerID
select new
{
o.CustomerID
}).Distinct();
Run Code Online (Sandbox Code Playgroud)
该查询带来了该Distinct子句的预期结果,但是CustomerID尽管我写了,但是没有订购orderby o.CustomerID!
第二个LINQ查询的SQL查询如下:
SELECT DISTINCT [t0].[CustomerID]
FROM [dbo].[Orders] AS [t0]
Run Code Online (Sandbox Code Playgroud)
我们可以看到**该ORDER BY条款缺失.这是为什么?
ORDER BY当我使用该Distinct() …
我正在使用WPF,我正在使用ListView,我需要在项目添加到它时触发事件.我试过这个:
var dependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(ListView));
if (dependencyPropertyDescriptor != null)
{
dependencyPropertyDescriptor.AddValueChanged(this, ItemsSourcePropertyChangedCallback);
}
Run Code Online (Sandbox Code Playgroud)
.....
private void ItemsSourcePropertyChangedCallback(object sender, EventArgs e)
{
RaiseItemsSourcePropertyChangedEvent();
}
Run Code Online (Sandbox Code Playgroud)
但它似乎只在整个集合发生变化时起作用,我已经阅读过这篇文章:event-fired-when-item-is-added-to-listview,但最佳答案仅适用于listBox.我试图将代码更改为ListView但我无法做到这一点.
我希望你能帮助我.先感谢您.
我正在使用MVVM在WPF中开发应用程序,但我坚持使用ICommand对象.
我有一个包含一些按钮的窗口,因此,我将它们绑定到XAML中各自的ICommand,如下所示:
<Button Command="{Binding DoSomethingCommand}" Content="Do Something" />
Run Code Online (Sandbox Code Playgroud)
然后,在我的视图模型类中,我写了以下内容:
public class MyViewModel : ObservableObject
{
private bool isDoSomethingButtonEnabled = false;
....
public ICommand DoSomethingCommand
{
get;
private set;
}
....
....
public MyViewModel()
{
DoSomethingCommand = new DelegateCommand<String>(this.OnDoSomething, this.CanDoSomething);
}
private void OnDoSomething(String arg)
{
}
private bool CanDoSomething(String arg)
{
return isDoSomethingButtonEnabled;
}
....
}
Run Code Online (Sandbox Code Playgroud)
所以,因为我需要在第一次打开窗口时没有启用我的按钮,所以我将变量设置isDoSomethingButtonEnabled为false.和它的作品,该按钮在开始时禁用的,但我的问题是,当我改变变量isDoSomethingButtonEnabled来true在运行时我的按钮仍然被禁用.
我甚至做了一些测试,改变变量后isDoSomethingButtonEnabled到true,印刷的结果DoSomethingCommand.CanExecute(),它显示了"真"!
所以,我该怎么办才能启用我的按钮?
先感谢您
我在C#工作,我有一个我只能使用的对象Reflection(出于某些个人原因).所以,当我需要为其中一个属性设置一些值时,我会按如下方式执行:
System.Reflection.PropertyInfo property = this.Parent.GetType().GetProperty("SomeProperty");
object someValue = new object(); // Just for example
property.SetValue(this.Parent, someValue, null);
Run Code Online (Sandbox Code Playgroud)
并且,为了获得它的价值,我使用了这个方法GetValue.
我的问题是:当使用Reflection更改属性时,是否有办法触发事件?
我正在研究 WPF,我正在创建一个 userControl,其中包含一个具有一些 TabItems 的 TabControl。
当所选的选项卡更改时,我需要执行一些东西,所以,我尝试做的是使用该事件,myTabControl.SelectionChanged但它是多次提出的,即使我只点击了一个tabItem。然后我读了这篇文章is-re-selected-tab-changed-event-in-the-standard-wpf-tab-control并将此代码放入我的方法中:
void mainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
//do work when tab is changed
}
}
Run Code Online (Sandbox Code Playgroud)
这样做之后,第一个问题已经解决,但是当我运行应用程序并尝试更改选项卡时,出现了一个错误:
Dispatcher processing has been suspended, but messages are still being processed
Run Code Online (Sandbox Code Playgroud)
Visual Studio 指向内部的第一行代码 if (e.Source is TabControl) { //here }
但是我发现这篇文章selectionchanged-event-firing-exceptions-for-unknown-reasons并且我可以通过编写一些代码来解决这个问题,如下所示:
void mainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
if (this.IsLoaded)
{
//do work when tab is changed
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在我遇到了另一个我无法解决的问题:
事件被触发两次!另一个奇怪的事情是,只有当我第一次尝试更改所选选项卡时,事件才会引发两次,但所选选项卡仍然相同 …
我正在开发一个WPF项目,每次有些userControl活动或非活动时我都会尝试触发事件.
这些userControl在其中有许多其他控件.
我想这使用用户控件的事件来实现GotFocus和LostFocus,但这些事件不会在我需要的方式工作,因为用户控件失去焦点时,我与它的内部控制工作.
所以,我的问题是:有没有办法保存userControl,因为Active当用户使用它内部的控件时,当用户转到另一个userControl时,第一个得到Inactive???
先感谢您.
我正在使用MVVM和WPF项目Microsoft Prism libraries.所以,当我需要通过类进行通信时,我使用该类Microsoft.Practices.Prism.MefExtensions.Events.MefEventAggregator,我发布事件和订阅方法如下:
要发布:
myEventAggregator.GetEvent<MyEvent>().Publish(myParams)
认购:
myEventAggregator.GetEvent<MyEvent>().Subscribe(MySubscribedMethod)
但我的问题是:有没有办法在发布事件后从"订阅方法"返回一些数据?
我正在一个WPF项目中,我正在创建一些样式,其中一种是DataGridCell样式,它可以正常工作。
我的问题是:当用户删除任何行时,Visual Studio的“输出”窗口中会显示许多错误。
这是错误:
System.Windows.Data Warning: 4 : Cannot find source for binding with reference
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid',
AncestorLevel='1''.
BindingExpression:Path=CanUserAddRows; DataItem=null; target element is 'DataGridCell'
(Name=''); target property is 'NoTarget' (type 'Object')
Run Code Online (Sandbox Code Playgroud)
因此,我猜错是因为,当DataGridCell从中删除时DataGrid,绑定找不到父对象,但是,我该怎么做才能避免出现这些错误?我的意思是,如何为绑定建立条件?
我的XAML样式代码如下:
<DataGrid Margin="6,25,6,35" x:Name="dataGrid">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=CanUserAddRows}" Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#A4A4A4"/>
</MultiDataTrigger>
. . . . .
Run Code Online (Sandbox Code Playgroud)
希望有人能帮助我,在此先感谢。
什么时候是使用 aTuple代替某种模型类的好地方?不就是懒得用吗Tuples?
我有时在项目的不同地方使用Tuplesor 有时dynamics,我问自己为每个创建一个模型类是否是一个很好的实践。
活动
\n\n我使用元组作为我在 PRISM 应用程序中编写的某些事件的有效负载,因为该类PubSubEvent仅采用一个通用类型参数。
解决方案:
\n\n这样做是一个好的做法吗?
\n\nJSON 参数解析
\n\n当我有一个 Web API 访问点时,我希望将 json 字符串作为参数,有时我会将字符串转换为动态类型并从中读取属性并将其传递给我的经理。
\n\n解决方案:
\n\n这样做是一个好的做法吗?
\n\n算法中的临时数据存储
\n\n在某些算法中,我使用Tuples临时存储数据。我认为在这种背景下Tuples是一件好事。
更多的?
\n\n还有更多Tuples有用的地方吗?什么时候应该使用它们,什么时候不应该使用它们?
微软软件定义网络
\n\n\n\n\n元组通常有四种使用方式:
\n\n\n
\n- 表示一组数据。例如,元组可以表示数据库记录,其组件可以表示记录的各个字段。
\n
当使用数据库框架时,我认为这不是一个好的做法?
\n\n\n\n
- 提供对数据集的轻松访问和操作。
\n …