您好我想从后面的代码访问ResourceDictinory.xaml文件中datatemplate内的控件。
我的ResFile1.xaml内容
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="btnTemplate1">
<StackPanel>
<TextBlock Name="txtBlock" Text="Abcd123" Foreground="Red"/>
<TextBox Name="txtBox" Text="textbox Text"/>
</StackPanel>
</DataTemplate>
好的。我在MainPage.xaml
中使用它 <phone:PhoneApplicationPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResFile1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button HorizontalAlignment="Left" Height="152" Margin="106,221,0,0"
VerticalAlignment="Top" Width="256"
ContentTemplate="{Binding ConverterParameter=blabla, Converter={StaticResource TestResConverter}, Mode=OneWay, Source={StaticResource btnTemplate1}}"/>
</Grid>
我的TestResConverter类。(访问此类中datatemplate内的元素)
public class ConverterTest : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is DataTemplate)
{
DataTemplate dataTemplate = value as DataTemplate;
//access elements
TextBox accessedTextbox = XXXMehod(dataTemplate);
accessedTextbox.Text = (string)parameter;//e.g change text property
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
如何访问datatemplate中的元素?什么是访问,编辑DataTemplate的替代方案?另外我看一下wpf项目,但是某些方法不存在wp7。
答案 0 :(得分:0)
使用ItemContainerGenerator
。
这是一个例子http://msdn.microsoft.com/en-us/library/bb613579.aspx