2015-04-03 49 views
-1

我是WPF和MVVM的新手,并试图遵循此设计,我创建了一个包含多个用户控件(每个控件10个)的窗口。这些用户控件将保存一个值,应该可以由用户输入并将其发送回数据库。在用户控件的实例中设置控件值WPF

我遇到的问题是我在Canvas中实际创建用户控件,不知道如何使用这些实例在我的View Model中设置控件上的值,其中SaveMethod绑定到Save按钮将数据保存到数据库中。谢谢您的帮助。

MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     ClientRatesViewModel viewModel = new ClientRatesViewModel(); 
     DataContext = viewModel; 
     viewModel.GetChargeUnits(); 

     int previousTopPreRate = 10; 
     foreach (var rate in viewModel.ClientRatesPreAwr) 
     { 
      PreAwr preAwr = new PreAwr(); 
      preAwr.tbPreAwrRate.Text = rate.ClientRatesPreAwr; 
      PreRatesCanvas.Children.Add(preAwr); 
      preAwr.Width = 500; 
      Canvas.SetLeft(preAwr, 10); 
      Canvas.SetTop(preAwr, previousTopPreRate + 10); 
      previousTopPreRate += +30; 
     } 

     int previousTopPostRate = 10; 
     foreach (var rate in viewModel.ClientRatesPostAwr) 
     { 
      PostAWR postAwr = new PostAWR(); 
      postAwr.tbPostAwrRate.Text = rate.ClientRatesPostAwr; 
      PostRatesCanvas.Children.Add(postAwr); 
      postAwr.Width = 500; 
      Canvas.SetLeft(postAwr, 10); 
      Canvas.SetTop(postAwr, previousTopPostRate + 10); 
      previousTopPostRate += +30; 
     } 
    } 
} 

ItemsControl的XAML:

<ItemsControl Name="icPreAwr" Margin="10,46,10,10"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid Margin="0,0,0,5"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="100" /> 
        </Grid.ColumnDefinitions> 
        <TextBlock Text="{Binding ClientRatesPreAwr }" /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
+0

删除所有代码并使用适当的XAML。使用ItemsControls(或派生控件,例如ListBoxes)来显示数据项的集合。从这里开始阅读:[数据模板概述](https://msdn.microsoft.com/en-us/library/ms742521.aspx)。 – Clemens 2015-04-03 18:29:58

+0

不是。不。 Nopenopenope。 Noooooope。这里是如何正确地做到这一点(不是100%符合你的问题,但按照你会明白):http://stackoverflow.com/a/29132115/1228 – Will 2015-04-06 15:23:18

+0

@Clemens谢谢,是可以添加一个用户控制到一个列表框,或者你的意思是在我的用户控件中创建包含数据的列表框项目? – 2015-04-08 11:43:21

回答

0

技术上你开始正确的,因为您所定义的视图和DataContext的。建议使用XAML,但是如果你开始使用,没有问题,并且与MVVM没有矛盾,因为后面的代码是View Code。

但是,在这里开始的问题,例如,而不是使用foreach,你应该使用ItemsControl和itemtemplate,它将是一个PreAwr和ItemsSource ClientRatesPreAwr。这将提供您的itemscontrol并通过itseld填充PreAwr PreAwr UserControl有一个tbPreAwrRate将内容设置为{Binding ClientRatesPreAwr},并且它将填充该值。

如果需要通过代码来使这一点,控件的属性是依赖属性,你可以通过代码使用

https://msdn.microsoft.com/en-us/library/cc838207%28v=vs.95%29.aspx

我希望它可以帮助你约束他们,我强烈建议你做步骤在XAML设计,如果该项目的规则可以以这种方式来完成

事实上我的文章可能是有用的,你http://bit.ly/1CoYRkQ

+0

谢谢你有一个例子,你可以提供使用我的代码呢?我正在努力做到这一点,我已经添加了我到目前为止的问题,谢谢 – 2015-04-08 11:40:15

0

对于任何阅读我设法实现通过在我的XAML中执行以下操作。

<ListBox ItemsSource="{Binding ClientRatesPreAwr}" 
     KeyboardNavigation.TabNavigation="Continue" Margin="0,58,0,69"> 
     <ItemsControl.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
      </Style> 
     </ItemsControl.ItemContainerStyle> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid Margin="2" Focusable="False"> 
        <UserControls:PreAwr /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ListBox>