2012-12-31 5 views
5

के साथ डब्ल्यूपीएफ डेटा बाध्यकारी मैं रिसोर्स डिक्शनरी के भीतर व्यूमोडेल के साथ एक दृश्य को बांधने की कोशिश कर रहा हूं लेकिन यह काम नहीं करता है।संसाधन डब्ल्यूवीवी

एप्लिकेशन 2 टेक्स्टबॉक्स के साथ बहुत ही सरल विंडो है। जब मैं टेक्स्टबॉक्स 1 पर टेक्स्ट टाइप करता हूं, तो अटूट रूप से, टेक्स्टबॉक्स 2 को वही टेक्स्ट मिलना चाहिए। बेशक व्यू से मेरे टेक्स्टबॉक्स को ViewModel में मेरी गुणों से जोड़ना होगा।

मैं WPF के लिए नया हूँ और जिस तरह से मैं दृश्य और ViewModels बाध्य करने के लिए शुरू कर दिया एक दृश्य के codebehind में था:

DataContext = new MyViewModel(); 

अब मैं एक क्लीनर जुदाई हासिल करने की कोशिश कर रहा हूँ।

<Application x:Class="NavigationCleanBinding.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     StartupUri="/Views/MainWindowView.xaml"> 
    <Application.Resources> 
     <ResourceDictionary Source="MainResourceDictionary.xaml" /> 
    </Application.Resources> 
</Application> 

MainResourceDictionary.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xamlpresentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Views="clr-namespace:NavigationCleanBinding.Views" 
    xmlns:ViewModels="clr-namespace:NavigationCleanBinding.ViewModels"> 

    <DataTemplate DataType="{x:Type ViewModels:MainWindowViewModel}"> 
     <Views:MainWindowView /> 
    </DataTemplate> 

</ResourceDictionary> 

MainWindowView.xaml: मेरी कोड

App.xaml है

<Window x:Class="NavigationCleanBinding.Views.MainWindowView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 

<Grid> 
    <TextBox Height="23" HorizontalAlignment="Left" Margin="61,14,0,0" 
      Name="textBox1" VerticalAlignment="Top" Width="120" 
      Text="{Binding TestData, Mode=TwoWay, 
      UpdateSourceTrigger=PropertyChanged}"/> 
    <Label Content="Test:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" 
     Name="label1" VerticalAlignment="Top" Width="43" /> 
    <Label Content="Result:" Height="28" HorizontalAlignment="Left" Margin="10,46,0,0" 
     Name="label2" VerticalAlignment="Top" /> 

    <TextBox Height="23" HorizontalAlignment="Left" Margin="61,48,0,0" 
      Name="textBox2" VerticalAlignment="Top" Width="120" 

      Text="{Binding TestData, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
    </Grid> 
</Window> 

MainWindowViewModel:

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

namespace NavigationCleanBinding.ViewModels 
{ 
    class MainWindowViewModel 
    { 
     private String _testData; 
     public String TestData 
     { 
      get { return _testData; } 
      set { _testData = value; } 
     } 

     private MainWindowViewModel() 
     { 
      _testData = null; 
     } 
    } 
} 

अद्यतन: इस तरह

public String TestData 
    { 
     get { return _testData; } 
     set 
     { 
      _testData = value; 
      OnPropertyChanged("TestData"); 

     } 
    } 

और implemened INotifyPropertyChanged:

मैं यह करने के लिए संपत्ति TestData बदल

public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
+0

क्या आपको अपनी आउटपुट विंडो में कोई त्रुटि मिल रही है? – Thelonias

+0

कोई त्रुटि नहीं, कोई चेतावनी नहीं। यह सिर्फ काम नहीं करता है। – shadox

+0

मुझे नहीं लगता कि आपका DataContext आपके 'MainWindowView' के लिए सेट किया जा रहा है। मुझे लगता है कि, 'MainWindowViewModel'' MainResourceDictionary' के लिए DataContext है। चूंकि आपके पास StartWURI MainWindowView पर सेट है, यह आपकी विंडो का एक उदाहरण बनाएगा, लेकिन डेटाकॉन्टेक्स्ट को कभी भी सेट नहीं करेगा। मैं सकारात्मक नहीं हूं, लेकिन मुझे नहीं लगता कि आपका संसाधन डिक्शनरी इस बिंदु पर कुछ भी कर रहा है। – Thelonias

उत्तर

4

तो user1064 519 सही रास्ते पर था: के रूप में यह MainWindow

  • ViewModel MainWindow में लोड करने की आवश्यकता है में होस्ट किया गया है

    • की जरूरत है दृश्य, एक UserControl, नहीं एक Window होने के लिए, यह क्या चलाता है DataTemplate खोज और लोड होने के लिए।

      <Window x:Class="WpfTemplateBootstrap.MainWindow" 
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:local="clr-namespace:WpfTemplateBootstrap" 
          Title="MainWindow" Height="350" Width="525"> 
          <ContentControl> 
           <ContentControl.Content> 
            <local:MainWindowViewModel /> 
           </ContentControl.Content> 
          </ContentControl> 
      

    उसके बाद आप और चल रहा होना चाहिए। मैंने यहां एक गहन उदाहरण पोस्ट किया है: wpf bootstrapping datatemplates--the chicken and the egg

  • +0

    मुझे लगता है कि TheZenker यहां से जुड़े ब्लॉग पर बहुत अच्छा प्रतिक्रिया देता है। – shadox

    1

    आपका ViewModel इंटरफ़ेस INotifyPropertyChanged को लागू करने और एक PropertyChanged घटना जब जुटाने चाहिए किसी भी बाध्य संपत्ति मूल्य में परिवर्तन, ताकि आपका विचार पता चले कि परिवर्तन ocurred।

    +0

    यह अभी भी काम नहीं करता है ... मेरा एक दोस्त रिश्तेदार स्रोत निर्दिष्ट करने का सुझाव देता है ... पूर्वजों के बारे में कुछ ... कि मैं वास्तव में समझ में नहीं आता। – shadox

    1

    डेटा टेम्पलेट sholudnt खिड़की में शामिल है, इसमें किसी भी प्रकार का नियंत्रण हो सकता है।

    DataTemplate:

    <DataTemplate DataType="{x:Type ViewModels:MainWindowViewModel}"> 
         <Views:MainWindowView /> 
    </DataTemplate> 
    

    UserControl:

    <UserControl x:Class="NavigationCleanBinding.Views.MainView" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Height="350" Width="525"> 
    
    <Grid> 
        <TextBox Height="23" HorizontalAlignment="Left" Margin="61,14,0,0" 
          Name="textBox1" VerticalAlignment="Top" Width="120" 
          Text="{Binding TestData, Mode=TwoWay, 
          UpdateSourceTrigger=PropertyChanged}"/> 
        <Label Content="Test:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" 
         Name="label1" VerticalAlignment="Top" Width="43" /> 
        <Label Content="Result:" Height="28" HorizontalAlignment="Left" Margin="10,46,0,0" 
         Name="label2" VerticalAlignment="Top" /> 
    
        <TextBox Height="23" HorizontalAlignment="Left" Margin="61,48,0,0" 
          Name="textBox2" VerticalAlignment="Top" Width="120" 
    
          Text="{Binding TestData, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
        </Grid> 
    </UserControl> 
    

    विंडो:

    <Window x:Class="NavigationCleanBinding.Views.MainWindowView" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525"> 
    
    <ContentControl Content={Binding}/> 
    </Window> 
    
    +0

    दरअसल यह काम नहीं करता है ... क्या आप मुझे अपना ऐप दिखा सकते हैं। एक्सएमएल? और कृपया बताएं कि मुख्य विंडो माई मुख्य दृश्य UserControl से कैसे जुड़ती है। धन्यवाद! – shadox

    +0

    DataTemplate के बारे में आपके UserControl को कैसे पता चला? और यदि यह कुछ संसाधन डिक्शनरी में है, तो UserControl को इसके बारे में पता होना चाहिए, है ना? –

    +0

    आप इसे संसाधन शब्दकोश या window.resources या app.resources में रख सकते हैं, यह आपकी आवश्यकताओं पर निर्भर करता है – user1064519