1. 内容控件通过ContentTemplate,列表控件通过itemTemplate 来支持模板绑定。例子如下:
模板定义:
xaml:
<DataTemplate x:Key="RadioSelector">
<Grid Width="500">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ParamName}" />
<RadioButton Grid.Column="1" IsChecked="{Binding ParamValue, Mode=TwoWay}" Content="True" />
<RadioButton Grid.Column="2" Content="False" HorizontalAlignment="Right" IsChecked="{Binding ParamValue, Mode=TwoWay, Converter={StaticResource StatusConvert}}"/>
</Grid>
</DataTemplate>
class:
public class StatusValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool status = (bool)value;
return !status;
}
public object ConvertBack(object value, Type targetType, object parameter,CultureInfo culture)
{
bool status = (bool)value;
return !status;
}
}