相关疑难解决方法(0)

在编译时检测到XAML已破坏的绑定

我认为这是一个简单而频繁的场景 - 您在XAML中将依赖属性绑定到viewmodel中的属性,然后在viewmodel中重命名该属性,并忘记在XAML中重命名它.结合破坏也是如此.

有没有办法在编译时检测到这样一个破坏的绑定?

data-binding wpf xaml mvvm

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

为什么这样做?

我正在谷歌搜索试图找到一种方法来调用Control.DataBindings.Add而不使用字符串文字,但从属性本身获取属性名称,我认为这不会出错,至少对于我的特定情况,因为我通常让Visual Studio重命名重命名属性时.所以我的代码看起来像是DataBindings.Add(GetName(myInstance.myObject)...代替DataBindings.Add("myObject"....所以我发现了这个:

    static string GetName<T>(T item) where T : class
    {
        var properties = typeof(T).GetProperties();
        if (properties.Length != 1) throw new Exception("Length must be 1");
        return properties[0].Name;
    }
Run Code Online (Sandbox Code Playgroud)

那将被称为,假设我有一个叫做的属性One,这样:string name = GetName(new { this.One });这会给我"One".我不知道它为什么会起作用以及使用它是否安全.我甚至都不知道这new { this.One }意味着什么.而且我不知道在哪种情况下它properties.Length不会发生.

顺便说一下,我只是测试将我的属性重命名OneTwoVisual Studio并将其new { this.One }转换为new { One = this.Two },当与GetName函数一起使用时"One",这使得整个事情变得毫无用处,因为我将要传递的名称Control.DataBindings.Add在重命名之后仍然是"One"属性.

.net c# reflection

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

ListBox.DisplayMember = [String]我可以以某种方式解决它不是.NET中的字符串吗?

询问有关对象数据源的问题.所以考虑一下我有一堂课

public class Customer{

    public String name;
    public int age;

    public Customer(String name, int age) {
         this.name = name;
         this.age = age;
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经将列表框数据绑定到这些对象的列表中.所以我说

listBox.DisplayMember = "name";
Run Code Online (Sandbox Code Playgroud)

但我的问题是,当我将Customer类的名称重构为

public String fullName;
Run Code Online (Sandbox Code Playgroud)

DisplayMember仍然保持"名称".这将失败.所以它降低了我重构域对象的能力.这有什么办法吗?

.net data-binding refactoring strong-typing

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

Xamarin.Forms:如何避免在MVVM绑定中对字符串进行硬编码

在Xamarin.Forms,我创建一个MVVM为绑定ButtonCommandProperty.

有没有办法避免硬编码属性名称的字符串?

这种方式有效,但我想避免硬编码字符串:

var submitButton = new Button();
submitButton.SetBinding(Button.CommandProperty, "SubmitButtonPressed");
Run Code Online (Sandbox Code Playgroud)

我在StackOverflow上找到了类似的答案,展示了如何在WinForms中执行类型安全,但Xamarin.Forms使用不同的绑定引擎.

c# binding mvvm xamarin xamarin.forms

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

使用mvvm模式的WPF数据绑定问题

我创建了一个用户控件"SearchControl"(它将在其他屏幕中进一步重用.SearchControl - >

<usercontrol name="SearchControl"......>
   <stackpanel orientation="horizontal"...>

       <TextBox Text"{Binding Path=UserId}"...>

       <Button Content="_Search" ....Command="{Binding Path=SearchCommand}"..>

   </stackpanel>
</usercontrol>

 public partial class SearchControl : UserControl
{
   public SearchControl()
   {
      InitializeComponent();
      DataContext=new UserViewModel();
   }
}
Run Code Online (Sandbox Code Playgroud)

然后我在窗口"UserSearch"中使用此控件

<window name="UserSearch".............
  xmlns:Views="Namespace.....Views">
  <Grid>
      <Grid.RowDefinitions>
         <RowDefinition..../>
         <RowDefinition..../>
         <RowDefinition..../>
         <RowDefinition..../>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
         <ColumnDefinition..../>
         <ColumnDefinition..../>         
      </Grid.ColumnDefinitions>

      <Views:SearchControl Grid.Row="0" Grid.Colspan="2"/>
      <TextBlock Text="User Id" Grid.Row="1" Grid.Column="0"..../>
      <TextBox Text="{Binding Path=UserId}" Grid.Row="1" Grid.Column="1".../>

      <TextBlock Text="First Name" Grid.Row="2" Grid.Column="0"..../>
      <TextBox Text="{Binding Path=FirstName}" Grid.Row="2" Grid.Column="1".../>

       <TextBlock Text="Last Name" Grid.Row="3" Grid.Column="0"..../>
       <TextBox Text="{Binding Path=LastName}" Grid.Row="3" Grid.Column="1".../>
  </Grid> …
Run Code Online (Sandbox Code Playgroud)

data-binding wpf mvvm

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