标签: binding

WPF MVVM从ViewModel在视图上触发事件的正确方法

在我的WPF应用程序中,我有2个Windows(两个Windows都有自己的ViewModel):

  1. 应用程序的主窗口,显示带有一堆单词的列表(绑定到MainViewModel)

  2. 允许用户将新项添加到列表中的对话窗口(绑定到AddWordViewModel)

MainViewModel具有List的Items属性(此集合由其中一个服务类填充)绑定到Main Window的ListBox

AddWordViewModel具有绑定到"添加单词对话框"的"保存"按钮的SaveWordCommand.它的任务是获取用户输入的文本并将其传递给服务类.

用户单击"保存"按钮后,我需要通知MainViewModel从服务中重新加载文章.

我的想法是在MainViewModel中公开public命令并从AddWordViewModel执行它

实施它的正确方法是什么?

谢谢!

.net wpf binding command mvvm

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

在Windows中使用特定的网络接口作为套接字

除了更改路由表之外,Windows中是否有可靠的方法来强制新创建的套接字使用特定的网络接口?据我所知,bind()接口的IP地址并不能保证这一点.

sockets windows networking binding bind

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

将listbox项中的命令绑定到viewmodel parent上的属性

我一直在研究这个问题大约一个小时,并查看了所有相关的SO问题.

我的问题很简单:

我有HomePageVieModel:

HomePageVieModel
+IList<NewsItem> AllNewsItems
+ICommand OpenNews
Run Code Online (Sandbox Code Playgroud)

我的加价:

<Window DataContext="{Binding HomePageViewModel../>
<ListBox ItemsSource="{Binding Path=AllNewsItems}">
 <ListBox.ItemTemplate>
   <DataTemplate>
       <StackPanel>
        <TextBlock>
           <Hyperlink Command="{Binding Path=OpenNews}">
               <TextBlock Text="{Binding Path=NewsContent}" />
           </Hyperlink>
        </TextBlock>
      </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

列表显示所有项目都很好,但对于我的生活,无论我尝试命令将无法工作:

<Hyperlink Command="{Binding Path=OpenNewsItem, RelativeSource={RelativeSource AncestorType=vm:HomePageViewModel, AncestorLevel=1}}">
<Hyperlink Command="{Binding Path=OpenNewsItem, RelativeSource={RelativeSource AncestorType=vm:HomePageViewModel,**Mode=FindAncestor}**}">
<Hyperlink Command="{Binding Path=OpenNewsItem, RelativeSource={RelativeSource AncestorType=vm:HomePageViewModel,**Mode=TemplatedParent}**}">
Run Code Online (Sandbox Code Playgroud)

我总是得到:

System.Windows.Data错误:4:无法找到带引用的绑定源.....

更新 我正在设置我的ViewModel吗?不认为这很重要:

 <Window.DataContext>
        <Binding Path="HomePage" Source="{StaticResource Locator}"/>
    </Window.DataContext>
Run Code Online (Sandbox Code Playgroud)

我使用MVVMLight工具包中的ViewModelLocator类来完成这项工作.

wpf binding mvvm icommand

20
推荐指数
4
解决办法
3万
查看次数

xjc:两个声明在ObjectFactory类中导致冲突

运行以下xjc命令会引发错误:

$ xjc "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd"
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 340 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

[ERROR] (Related to above error) This is the other declaration.   
  line 475 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd
Run Code Online (Sandbox Code Playgroud)

虽然我理解JAXB绑定以及XJC中的冲突是什么,但我不明白当前模式中的冲突在哪里.

我应该怎么解决这个问题?

谢谢,

皮埃尔

更新:这里是错误的上下文:

$ curl -s "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd" | sed 's/^[ \t]*//' | cat -n | egrep -w -A 10 -B 10 '(340|475)' 
   330  <xs:element maxOccurs="1" name="Description"
   331  type="xs:string" minOccurs="0">
   332  <xs:annotation>
   333  <xs:documentation>
   334  Optionally provide description especially …
Run Code Online (Sandbox Code Playgroud)

java schema binding xsd xjc

20
推荐指数
2
解决办法
4万
查看次数

如何在 RecyclerView 中使用 Android 视图绑定

这不是数据绑定,这是此处描述的 Android Studio 3.6 Canary 11+ 新增的视图绑定。

对于在 Activity 中使用,很明显,您只需像这样使用它:

假设我们有一个名为的布局 activity_main.xml

然后在代码中我们可以这样使用它:

public class MainActivity extends Activity{

    ActivityMainBinding binding; //Name of the layout in camel case + "Binding"

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,不清楚如何将 View Binding 与 RecyclerView 一起使用?

编辑:请用Java解释。

java xml binding android view

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

Raku 中的方法/子绑定

我想知道是否有办法将方法和/或子绑定到 Raku 中的另一个方法/子名称。我已经看到如何将变量绑定到方法/子,但这并不是我想要的。我知道如何在 Perl 5 中做到这一点:

sub sub1 {
  print "sub1!\n";
}

*sub2 = \&sub1;

sub1(); # sub1!
sub2(); # sub1!
Run Code Online (Sandbox Code Playgroud)

methods binding alias function raku

20
推荐指数
2
解决办法
324
查看次数

XAML:将文本框maxlength绑定到Class常量

我试图将WPF文本框的Maxlength属性绑定到类中的已知常量.我正在使用c#.

该类的结构与以下内容差别不大:

namespace Blah
{
    public partial class One
    {
        public partial class Two
        {
             public string MyBindingValue { get; set; }

             public static class MetaData
             {
                 public static class Sizes
                 {
                     public const int Length1 = 10;
                     public const int Length2 = 20;
                 }
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是的,它是深层嵌套的,但不幸的是在这种情况下,如果不需要大量重写,我就无法移动.

我希望我能够将文本框MaxLength绑定到Length1或Length2值,但我无法让它工作.

我期待绑定类似于以下内容:

<Textbox Text="{Binding Path=MyBindingValue}" MaxLength="{Binding Path=Blah.One.Two.MetaData.Sizes.Length1}" />
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

非常感谢

xaml binding textbox constants maxlength

19
推荐指数
2
解决办法
9061
查看次数

CollectionViewSource仅在第一次绑定到源时进行排序

我正在使用绑定到CollectionViewSource(播放器)的DataGrid ,它本身绑定到ListBox(级别)的当前所选项目,每个项目包含要在DataGrid中排序/显示的集合:

<ListBox Name="lstLevel"
         DisplayMemberPath="Name" 
         IsSynchronizedWithCurrentItem="True" />
Run Code Online (Sandbox Code Playgroud)

...

<!-- DataGrid source, as a CollectionViewSource to allow for sorting and/or filtering -->
<CollectionViewSource x:Key="Players" 
                      Source="{Binding ElementName=lstLevel, 
                                       Path=SelectedItem.Players}">
  <CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="Name" />
  </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

...

  <DataGrid Name="lstPlayers" AutoGenerateColumns="False" 
            CanUserSortColumns="False"
            ItemsSource="{Binding Source={StaticResource Players}}">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Name"
                          Binding="{Binding Path=Name, Mode=TwoWay}"
                          Width="*" />
      <DataGridTextColumn Header="Age"
                          Binding="{Binding Path=Age, Mode=TwoWay}"
                          Width="80">
      </DataGridTextColumn>
    </DataGrid.Columns>
  </DataGrid>
Run Code Online (Sandbox Code Playgroud)

(整个C#代码在这里,XAML代码在这里,整个测试项目在这里 - 除了DataGrid我已经为玩家添加了一个简单的ListBox,以确保它不是DataGrid问题)

问题是玩家在第一次显示时进行排序,但是一旦我从ListBox中选择另一个级别,他们就不再排序了.此外,在第一次显示玩家时修改名称将根据更改对其进行排序,但是一旦更改了级别,就不再对其进行排序.

所以看起来改变CollectionViewSource的源代码会以某种方式破坏排序功能,但我不知道为什么,也不知道如何修复它.有谁知道我做错了什么?

(我用过滤器进行了测试,但是那个按预期工作了)

该框架是.NET 4.

wpf binding collectionviewsource

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

WPF事件绑定到ViewModel(对于非Command类)

我正在使用应用程序的第二个版本,作为重写的一部分,我必须转向MVVM架构.我正面临压力,要在视图模型类中放置绝对的所有代码 - 在代码隐藏文件中使用c#是不受欢迎的.(我知道,我知道......我知道背后的代码不是坏事,但这次不是我的电话).

对于实现命令界面的对象,这很容易.我已经能够找到大量关于如何将这些对象的Command绑定到视图模型中的ICommand的信息.问题是没有这种接口的对象,例如

<ListBox
   x:Name="myListBox"
   MouseDoubleClick="myCallbackFunction">

<!-- ... -->

</ListBox>
Run Code Online (Sandbox Code Playgroud)

我想知道如何将Listbox的MouseDoubleClick事件绑定到myCallbackFunction,这是在视图模型中实现的.这甚至可能吗?

谢谢!

c# xaml binding mvvm viewmodel

19
推荐指数
2
解决办法
4万
查看次数

角度2:动态图像

imgAngular2中设置标记的最佳方法是什么?

这不起作用(而不是因为键):

<img src="//maps.googleapis.com/maps/api/streetview?size=400x400&location="{{property.lat}}, {{property.long}}"key=aiwefmboij234blahblah" />
Run Code Online (Sandbox Code Playgroud)

我们如何将lat/long值传入src属性?

谢谢!

binding image angular

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