2015-04-03 79 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>