小编Rac*_*hel的帖子

WPF - ControlTemplate上的事件?

有谁知道为什么我不能在控件模板上设置事件?

例如,以下代码行将无法编译.它通过控件模板中的任何事件执行此操作.

<ControlTemplate x:Key="DefaultTemplate" TargetType="ContentControl">
   <StackPanel Loaded="StackPanel_Loaded">

   </StackPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我使用的是MVVM设计模式,此处的控件位于ResourceDictionary中,该ResourceDictionary被添加到应用程序的MergedDictionaries中.

wpf controltemplate routed-events

9
推荐指数
1
解决办法
7594
查看次数

为什么我在System.Data.Entity.dll(实体框架)中收到NullReferenceException?

我有一个List<State>State实体对象,每个State对象具有其他对象的集合,诸如Licenses,Taxes,Bonds

还有一个集合ExpiredObjects,它是任何已过期且需要更新的对象的列表.出于某种原因,这个属性给了我一个NullReferenceException当我尝试访问它为一个特定的状态(内华达州),但我不能为我的生活弄清楚究竟是什么,null因为我没有看到任何null值的任何地方.

这是我抛出异常的代码.它遍历所有状态,并将所有状态添加ExpiredObjects到特定于视图的集合中.我的测试代码仍然包含在内

    private List<ExpiredObject> LoadAllExpiredObjects()
    {
        var list = new List<ExpiredObject>();
        foreach (var state in States)
        {
            // This tells me the state is not null, and neither is state.ExpiredObjects
            Debug.WriteLine(string.Format("{0}|{1}|{2}", 
                state.Name, state == null, state.ExpiredObjects == null));

            try
            {
                var x = state.ExpiredObjects;
                Debug.WriteLine(x == null);

                // Exception occurs on the "for" line on the Nevada …
Run Code Online (Sandbox Code Playgroud)

c# wpf entity-framework

9
推荐指数
1
解决办法
1万
查看次数

为什么这个额外的空间出现在网格中?

我正在看这个问题并发现了一些非常奇怪的东西:在某些情况下,似乎错误地计算了一行的高度Grid.RowSpan.

这是Grid我正在测试的简单图纸:

---------------
|   1   |     |
--------|  3  |
|   2   |     |
---------------
|      4      |
---------------

以下是此网格的一些示例代码,用于演示此问题:

<Grid ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Background="Red">
        <Label Content="CELL 1 A"/>
        <Label Content="CELL 1 B"/>
        <Label Content="CELL 1 C"/>
    </StackPanel>

    <Grid Grid.Column="0" Grid.Row="2" Background="CornflowerBlue">
        <Label Content="CELL 2 D"/>
    </Grid>

    <StackPanel Grid.Column="1" Grid.Row="0" …
Run Code Online (Sandbox Code Playgroud)

wpf grid

9
推荐指数
1
解决办法
993
查看次数

是否有任何理由在此代码块中的List <T>中找不到现有项?

我们有一个绑定到List<T>项目的网格.只要用户点击"刷新",就会从数据库中获取更改,并更新绑定列表.我遇到了一个问题,即重复项目被添加到网格中,我无法弄清楚原因.

数据库调用返回两个值:List<int>已更改的记录ID,以及已更改记录List<MyClass>的更新数据.我正在调试的现有代码代码找出需要更新的内容,如下所示:

public void FindUpdates(IList<MyClass> existingRecords,
                    IList<MyClass> updatedRecords,
                    List<int> updatedIds,
                    out IDictionary<int, int> existing,
                    out IDictionary<int, int> updated,
                    out List<int> updates,
                    out List<int> removes,
                    out List<int> adds)
{
    updates = new List<int>();
    removes = new List<int>();
    adds = new List<int>();

    existing = FindUpdated(existingRecords, updatedIds);
    updated = FindUpdated(updatedRecords, updatedIds);

    if (updatedIds != null)
    {
        // split add/update and remove
        foreach (int id in updatedIds)
        {
            if (!existing.ContainsKey(id))
                adds.Add(id);
            else if (updated.ContainsKey(id))
                updates.Add(id);
            else
                removes.Add(id);
        } …
Run Code Online (Sandbox Code Playgroud)

c# .net-3.5 devexpress-windows-ui

9
推荐指数
1
解决办法
496
查看次数

有没有办法在wpf WebBrowser控件之上呈现WPF控件?

我需要在我的应用程序中使用嵌入式WebBrowser控件,并且在显示WPF内容时遇到问题.应用程序有时会显示用于编辑数据或显示错误的弹出窗口,并且WebBrowser会在弹出窗口之上绘制,因为它是WinForms控件.

我在这里看到的替代方法使用Popup控件将项目放在WebBrowser控件的顶部,但是我的问题是Popups在切换到另一个应用程序时保持打开状态,并且当用户调整大小/移动时它们不随应用程序移动该应用程序.

有没有其他方法可以做到这一点?嵌入式Web内容是aspx页面,因此不是静态HTML.

browser wpf xaml

8
推荐指数
1
解决办法
6520
查看次数

在哪里可以找到WPF DataGrid的XAML模板?

我想修改Microsoft WPF DataGrid(4.0版本)的模板,但我不确定现有模板是什么样的.有谁知道它是什么或我在哪里可以找到它?

wpf xaml datagrid templates

8
推荐指数
2
解决办法
1万
查看次数

是否有理由让每个WCF调用异步?

是否有理由让每个WCF服务调用异步?

我和我的伙伴正在讨论这个问题.他想让每个WPF服务调用Async以避免锁定UI(它是桌面WPF应用程序).我反对这个想法.在大多数情况下,我不认为需要异步调用,并且在需要时,应该专门编码RequestingClass和DataManager来处理异步调用.

我的论点是,为所有事情设置回调的代码要多得多,而且非常令人困惑.我也认为这可能会导致性能下降,尽管我尚未对此进行验证.他的论点是,有时候你会收到大量数据并且会锁定用户界面,并且设置这样的WCF调用并不是那么多工作(他也没有发现下面的代码令人困惑).

我们之前从未使用过WCF服务器,所以我想我会给他带来疑问,并在这里询问其他一些意见.

例如:

我的方式:

public override User GetById(int id)
{
    return new User(service.GetUserById(id));
}
Run Code Online (Sandbox Code Playgroud)

它锁定UI,UserDataManager和WCF服务通道,直到WCF服务器返回User DataTransferObject,但它易于理解并快速编码.它将用于大多数WCF服务调用,除非它实际上预期获取数据的延迟,在这种情况下DataManager将被设置为处理异步调用.

他的方法:

public override void GetById(int id, Action<UserGroup> callback = null)
{
    // This is a queue of all callbacks waiting for a GetById request
    if (AddToSelectbyIdQueue(id, callback))
        return;

    // Setup Async Call
    var wrapper = new AsyncPatternWrapper<UserDTO>(
        (cb, asyncState) => server.BeginGetUserById(id, cb, asyncState),
        Global.Instance.Server.EndGetUserById);

    // Hookup Callback
    wrapper.ObserveOnDispatcher().Subscribe(GetByIdCompleted);

    // Run Async Call
    wrapper.Invoke();
}

private void GetByIdCompleted(UserDTO dto)
{
    User user = …
Run Code Online (Sandbox Code Playgroud)

c# wcf asynchronous

8
推荐指数
1
解决办法
1561
查看次数

当Row比Viewport高时,如何让DataGrid平滑滚动?

我有一个充满笔记的DataGrid,并且笔记可能比DataGrid的高度更高.发生这种情况时,如果您尝试向下滚动以阅读注释的下半部分,DataGrid会立即跳到下一行.

这不仅会阻止用户查看完整音符,还会导致滚动感觉不稳定,因为它似乎会跳转.

有没有办法告诉WPF在不禁用默认DataGrid虚拟化的情况下顺畅地滚动长音符?

wpf datagrid

8
推荐指数
1
解决办法
4384
查看次数

我正在将我的EF linq查询更改为存储过程.有没有简单的方法来设置复杂的映射?

我有一个包含5个独立表的Entity Framework项目.当我从这些表中查询和使用数据时.Include(),它生成的查询非常慢并且超时.

我已将查询移动到存储过程,现在正在寻找一种查询此存储过程的方法,并轻松地将它返回的数据映射到现有数据类.

我对EF的DataContext的原始Linq查询如下所示:

var data = (from a in context.A
                    .Include("B")
                    .Include("C")
                    .Include("D")
                    .Include("E")
            where a.Id == someValue
            select a);
Run Code Online (Sandbox Code Playgroud)

它返回一个类似于此的Entity数据对象:

class A
{
    int Id;
    string otherProperties;

    List<B> B;
    List<C> C;
    List<D> D;
    List<E> E;
}
Run Code Online (Sandbox Code Playgroud)

EF生成在SQL服务器上运行的查询返回的结果集如下所示:

C1    C2    C3    C4    C5    C6    C7    C8    C9    C10
----------------------------------------------------------
A1    A2    B1    B2        
A1    A2                C1    C2
A1    A2                C1    C2
A1    A2                C1    C2
A1    A2                            D1    D2
A1    A2                            D1    D2
A1    A2                            D1    D2 …

c# entity-framework entity-framework-4

8
推荐指数
1
解决办法
1305
查看次数

我可以在不参考类型的情况下反序列化泛型吗?

我试图将类保存/加载到包含使用a的泛型类型的xml文件DataContractSerializer.我有保存工作,但已经意识到我无法加载它,因为我没有解串器的knownTypes列表.

有没有一种序列化/反序列化这个类的方法,允许我反序列化它而不直接引用任何存储的类型?

这是我SessionVariables要保存/加载的课程:

[DataContract]
public class SessionVariables
{
    [DataMember]
    private Dictionary<Type, ISessionVariables> _sessionVariables = new Dictionary<Type, ISessionVariables>();
    private object _syncLock = new object();

    public T Get<T>()
        where T : ISessionVariables, new()
    {
        lock (_syncLock)
        {
            ISessionVariables vars = null;

            if (_sessionVariables.TryGetValue(typeof(T), out vars))
                return (T)vars;

            vars = new T();
            _sessionVariables.Add(typeof(T), vars);

            return (T)vars;
        }
    }

    public IList<Type> GetKnownTypes()
    { 
        IList<Type> knownTypes = new List<Type>();

        knownTypes.Add(this.GetType().GetType()); // adds System.RuntimeType

        foreach (Type t in _sessionVariables.Keys)
        {
            if (!knownTypes.Contains(t)) …
Run Code Online (Sandbox Code Playgroud)

c# datacontractserializer .net-3.5

8
推荐指数
1
解决办法
1213
查看次数