小编Edw*_*uay的帖子

更新ViewModel属性时如何避免递归?

在我的视图中,我有一个滑块和一个组合框.

当我更改滑块时,我希望组合框改变.

当我更改组合框时,我想要更改滑块.

我可以使用其中一个,但如果我尝试更新两个,我会收到StackOverflow错误,因为一个属性在无限循环中不断更新另一个属性.

我已经尝试进入一个Recalculate(),其中更新在一个地方完成,但仍然遇到递归问题.

如何在不进行递归的情况下让每个控件更新另一个?

在视图中:

<ComboBox 
    ItemsSource="{Binding Customers}"
    ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
    Margin="20"
    HorizontalAlignment="Left"
    SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>


<Slider Minimum="0" 
        Maximum="{Binding HighestCustomerIndex, Mode=TwoWay}" 
        Value="{Binding SelectedCustomerIndex, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)

在ViewModel中:

#region ViewModelProperty: SelectedCustomer
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
    get
    {
        return _selectedCustomer;
    }

    set
    {
        _selectedCustomer = value;
        OnPropertyChanged("SelectedCustomer");
        SelectedCustomerIndex = _customers.IndexOf(_selectedCustomer);
    }
}
#endregion

#region ViewModelProperty: SelectedCustomerIndex
private int _selectedCustomerIndex;
public int SelectedCustomerIndex
{
    get
    {
        return _selectedCustomerIndex;
    } …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf mvvm

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

如何告诉C#查看属性的对象基类?

我在下面的指定行中收到错误" T不包含Id的定义 ",即使在我调试时,我看到"item" 确实在其基类中具有属性"Id" .

我如何在这里指定我希望C#在项目的基类中查找Id(为什么不自动执行此操作?)?

//public abstract class Items<T> : ItemBase (causes same error)
public abstract class Items<T> where T : ItemBase
{
    public List<T> _collection = new List<T>();
    public List<T> Collection
    {
        get
        {
            return _collection;
        }
    }

    public int GetNextId()
    {
        int highestId = 0;
        foreach (T item in _collection)
        {
           //ERROR: "T does not contain a definition for Id
           if (item.Id > highestId) highestId = item.Id; 
        }

        return highestId;
    }

}
Run Code Online (Sandbox Code Playgroud)

以下是如何定义类:

public …
Run Code Online (Sandbox Code Playgroud)

c# generics inheritance

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

找出字符串是否以另一个字符串结尾的最快方法是什么?

这种加速方法的最佳重写是什么?

public static bool EndsWith(string line, string term)
{

    bool rb = false;

    int lengthOfTerm = term.Length;

    string endOfString = StringHelpers.RightString(line, lengthOfTerm);

    if (StringHelpers.AreEqual(term, endOfString))
    {
        return true;
    }
    else
    {
        rb = false;
    }

    if (line == term)
    {
        rb = true;
    }

    return rb;

}
Run Code Online (Sandbox Code Playgroud)

.net c# string

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

比较两个以逗号分隔的字符串匹配的最短代码是什么?

下面称为UserCanAccessThisPage的方法基于以下逻辑:每个用户和每个页面都有一个组列表.如果其中任何一个匹配,则用户可以访问该页面.

下面的代码做了我想要的,但我的解决方案是非常C#1(或C#2,至少我没有使用ArrayList).

任何人都可以重构这一点,这样更直接,例如使用lambdas来消除这两种方法吗?我只是无法获得语法来做到这一点.

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestCompare2343
{
    class Program
    {
        static void Main(string[] args)
        {

            string anonymousUserAccessGroups = "loggedOutUsers";
            string normalUserAccessGroups = "loggedInUsers, members";
            string developerUserAccessGroups = "loggedInUsers, members, administrators, developers";

            string loginPageAccessGroups = "loggedOutUsers";
            string logoutPageAccessGroups = "loggedInUsers";
            string memberInfoPageAccessGroups = "members";
            string devPageAccessGroups = "developers";

            //test anonymousUser
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, loginPageAccessGroups));
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, logoutPageAccessGroups));
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, memberInfoPageAccessGroups));
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(anonymousUserAccessGroups, devPageAccessGroups));
            Console.WriteLine("---");

            //test anonymousUser
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, loginPageAccessGroups));
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, logoutPageAccessGroups));
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, memberInfoPageAccessGroups));
            Console.WriteLine(StringHelpers.UserCanAccessThisPage(normalUserAccessGroups, devPageAccessGroups));
            Console.WriteLine("---"); …
Run Code Online (Sandbox Code Playgroud)

c# lambda

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

如何单独设置StackPanels边距?

我可以在代码隐藏中设置stackpanel的边距,如下所示:

StackPanel sp2 = new StackPanel();
sp2.Margin = new System.Windows.Thickness(5);
Run Code Online (Sandbox Code Playgroud)

但是如何单独设置,这两个都不起作用:

伪代码:

sp2.Margin = new System.Windows.Thickness("5 0 0 0");
sp2.Margin.Left = new System.Windows.Thickness(5);
Run Code Online (Sandbox Code Playgroud)

c# wpf margin code-behind stackpanel

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

将Observable对象集合保存到XML文件的最简单方法是什么?

我有一个包含客户对象的Observable集合:

  public class Customer
  {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string Location { get; set; }
    public string ZipCode { get; set; }
  }
Run Code Online (Sandbox Code Playgroud)

将此转储到XML文件的最简单方法是什么,以便稍后阅读?

c# xml observablecollection

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

如何为LINQ创建的新对象提供替代名称?

如何为customers变量的属性指定特定名称,例如:

伪代码:

var customers = from c in Customers
    where c.Orders.Count > 15
    orderby c.Orders.Count ascending
    select new { 
        c.ContactName as Name, 
        c.City, 
        c.Orders.Count as NumberOfOrders };
Run Code Online (Sandbox Code Playgroud)

linq

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

我怎么用?把这两行合二为一?

我想将下面的两个属性赋值行放到一行中,因为我要将它们构建到一个应用程序中,它们将会很多.

有没有办法在一行优雅构造的C#中表达这两行,也许有一个?? 像这样的运营商?

string nnn = xml.Element("lastName").Attribute("display").Value ?? "";
Run Code Online (Sandbox Code Playgroud)

这是代码:

using System;
using System.Xml.Linq;

namespace TestNoAttribute
{
    class Program
    {
        static void Main(string[] args)
        {

            XElement xml = new XElement(
                new XElement("employee",
                    new XAttribute("id", "23"),
                    new XElement("firstName", new XAttribute("display", "true"), "Jim"),
                    new XElement("lastName", "Smith")));

            //is there any way to use ?? to combine this to one line?
            XAttribute attribute = xml.Element("lastName").Attribute("display");
            string lastNameDisplay = attribute == null ? "NONE" : attribute.Value;

            Console.WriteLine(xml);
            Console.WriteLine(lastNameDisplay);

            Console.ReadLine();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c#

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

如何删除 WPF 工具栏托盘上的下拉图标?

如何摆脱下面工具栏项中文本右侧的灰色下拉区域?

替代文本

这是生成它的代码:

ToolBar tb = new ToolBar();
tb.Background = new SolidColorBrush(Colors.Transparent);
Button button = new Button();
button.Content = "test";
button.Click += new RoutedEventHandler(button_Click);
tb.Items.Add(button);
value.ToolBars.Add(tb);
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml toolbar

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

阻止垃圾邮件发送者的IP地址也会阻止访问者?

我的网站的评论功能不断受到来自这两个IP地址的垃圾邮件的轰炸:

  • 94.102.63.11
  • 83.233.30.42

我目前正在阻止他们在文本级别,因为他们都有:

weebly.com

在它们中,我认为如果我通过IP地址阻止它们,那么我也可以阻止真实用户.

  1. 阻塞IP地址是否也可以阻止具有相同IP地址的真实用户,例如使用拨号提供商?

  2. 我怎样才能知道谁在运行这些垃圾邮件服务?

  3. 我如何能够帮助将这些IP地址报告给负责防止互联网上垃圾邮件的中央机构?

ip-address spam-prevention spam

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