के साथ डब्ल्यूपीएफ डेटा बाध्यकारी मैं रिसोर्स डिक्शनरी के भीतर व्यूमोडेल के साथ एक दृश्य को बांधने की कोशिश कर रहा हूं लेकिन यह काम नहीं करता है।संसाधन डब्ल्यूवीवी
एप्लिकेशन 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));
}
}
क्या आपको अपनी आउटपुट विंडो में कोई त्रुटि मिल रही है? – Thelonias
कोई त्रुटि नहीं, कोई चेतावनी नहीं। यह सिर्फ काम नहीं करता है। – shadox
मुझे नहीं लगता कि आपका DataContext आपके 'MainWindowView' के लिए सेट किया जा रहा है। मुझे लगता है कि, 'MainWindowViewModel'' MainResourceDictionary' के लिए DataContext है। चूंकि आपके पास StartWURI MainWindowView पर सेट है, यह आपकी विंडो का एक उदाहरण बनाएगा, लेकिन डेटाकॉन्टेक्स्ट को कभी भी सेट नहीं करेगा। मैं सकारात्मक नहीं हूं, लेकिन मुझे नहीं लगता कि आपका संसाधन डिक्शनरी इस बिंदु पर कुछ भी कर रहा है। – Thelonias