1. Binding中的Path表示该控件绑定到的类中的哪个属性是该控件需要关心的。
2. Binding中的ElementName属性或RleativeSource属性指定绑定源。
//
前置声明:代码中出现的"..."是我省略的不在绑定中不重要的代码,目的是为提取出主干方便理解。
1. 基本绑定
参考资料1:http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh758320.aspx#Y921
功能:将滚动条和一个文本块TextBlock绑定在一起。
实现代码:
<TextBox Text="{Binding ElementName=slider1, Path=Value, Mode=OneWay}" ... />
<Slider x:Name="slider1" ... />
参数解释:
ElementName: 和该TextBox绑定的其他控件名称
Path: 表示将TextBox的Text文本应该显示的值绑定到Value这个属性。
Mode: 绑定方式,确定数据流动的方式和时间。具体可“参考资料1”里面的链接。
2. 模板绑定
参考资料2:http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5/
请下C++的。
<ListBox x:Name="Scenarios" ... >
<ListBox.ItemTemplate>
<DataTemplate>
<!-- 这里你不添加任何东西都可以,只要你将该ListBox的ItemSource中的各个Item类型指定为
ListBoxItem -->
<!--<TextBlockText="{Binding Path=Content}"/>-->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
本来我还以为被注视的<TextBlock>会起什么作用,结果即使我删除了,结果还是正常。
为什么?因为它是用下面的方法创建TextBlock的:
// Populate the ListBox with the list of scenarios as defined in Constants.cpp.
for (unsigned int i = 0; i < scenarios->Length; ++i) {
Scenario s = scenarios[i];
ListBoxItem^ item = ref new ListBoxItem();
item->Name = s.ClassName;
item->Content = (i + 1).ToString() + ") " + s.Title;
ScenarioList->Append(item);
}
// Bind the ListBox to the scenario list.
Scenarios->ItemsSource = ScenarioList;
Scenarios->ScrollIntoView(Scenarios->SelectedItem);
3. 个性化绑定(个人命名)
参考资料3:http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh758320.aspx#Y921
先看XAML中的代码:
<TextBox x:Name="TextBox_1" Text="{Binding Path=Name}" ... />
<Button Click="Button_Click_1" .../>
很简单的一个绑定,这里可能需要问Path=Name是什么意思?其实我还想提出一个概念,就是DataContext的含义。这个
DataContext就是源,说白了就是“该TextBox数据的来源”。而这个Name就是数据来源的一个属性,举个例子假设你调用
TextBox_1->DataContext = class1;那么这里的class1一般是一个类,而这个Name就是class1的一个属性,该属性的任何变
化都会导致TextBox_1的Text文本内容改变。
下面简单说下如何在一个Blank App(XAML)中使用这个个性化绑定。
<3.1>在MainPage.xaml中加入2个控件。
<!-- 个性化绑定 -->
<TextBox x:Name="TextBox_1" Text="{Binding Path=Name}" HorizontalAlignment="Left" Margin="72,508,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="214" Height="49"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="72,562,0,0" VerticalAlignment="Top"
Height="70" Width="214" Click="Button_Click_1"/>
<3.2>在MainPage.xaml.h中加入
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml::Data;
//
// TextBox的绑定
[Windows::Foundation::Metadata::WebHostHidden]
[Windows::UI::Xaml::Data::Bindable] // in c++, adding this attribute to ref classes enables
data binding for more info search for 'Bindable' on the page http://go.microsoft.com/fwlink/?LinkId=254639
public ref class Employee sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
{
public:
property Platform::String^ Name
{
Platform::String^ get()
{
return _name;
}
void set(Platform::String^ value)
{
_name = value;
Employee::OnPropertyChanged("Name");
}
}
private:
Platform::String^ _name;
#pragma region INotifyPropertyChanged
private:
bool _isPropertyChangedObserved;
event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ _privatePropertyChanged;
protected:
/// <summary>
/// Notifies listeners that a property value has changed.
/// </summary>
/// <param name="propertyName">Name of the property used to notify listeners.</param>
void OnPropertyChanged(String^ propertyName)
{
if (_isPropertyChangedObserved)
{
PropertyChanged(this, ref new PropertyChangedEventArgs
(propertyName));
}
}
public:
Employee()
{
_isPropertyChangedObserved = false;
}
// in c++, it is not neccessary to include definitions of add, remove, and raise.
// these definitions have been made explicitly here so that we can check if the
// event has listeners before firing the event
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
{
virtual Windows::Foundation::EventRegistrationToken add
(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e)
{
_isPropertyChangedObserved = true;
return _privatePropertyChanged += e;
}
virtual void remove(Windows::Foundation::EventRegistrationToken t)
{
_privatePropertyChanged -= t;
}
protected:
virtual void raise(Object^ sender,
Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
{
if (_isPropertyChangedObserved)
{
_privatePropertyChanged(sender, e);
}
}
}
#pragma endregion
};
<3.3>继续在public ref class MainPage sealed类里面加入
public:
void Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
private:
// 动态绑定TextBlock
void employeeChanged(Object^ sender, PropertyChangedEventArgs^ e);
property Employee^ _employee;
<3.4>在MainPage::MainPage()里面加入
// 个性化绑定--动态绑定TextBlock的数据
_employee = ref new Employee();
TextBox_1->DataContext = _employee;
_employee->PropertyChanged += ref new PropertyChangedEventHandler(this, &MainPage::employeeChanged);
_employee->Name = "Jane Doe";
<3.5>在MainPage.xaml.cpp加入
void MainPage::employeeChanged(Object^ sender, PropertyChangedEventArgs^ e)
{
}
void MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
_employee->Name += TEXT(" name");
}
然后编译,单击Button,发现TextBox会添加name。
注1:当你希望多个绑定使用同一个源时,设置数据上下文将十分有用。若要为单个绑定设置源,请设置 Binding 对象上的
Source 属性。
注2:具体信息可参考“参考资料3”
注3:同理你要是将TextBox的Path=Name改为Path=Name6,那么就需要给将要绑定的类加一个Name6属性了。