C# 如何从外部设置用户控件的属性?
这是我的XAML:C# 如何从外部设置用户控件的属性?,c#,wpf,xaml,C#,Wpf,Xaml,这是我的XAML: <UserControl x:Class="SearchResultsBox"> <ListBox ItemsSource="{Binding SearchResults}" SelectedItem="{Binding Selected}" Style="{StaticResource ListBoxStyle1}" ItemContainerStyle="{StaticResource SearchIt
<UserControl x:Class="SearchResultsBox">
<ListBox ItemsSource="{Binding SearchResults}" SelectedItem="{Binding Selected}"
Style="{StaticResource ListBoxStyle1}"
ItemContainerStyle="{StaticResource SearchItemContainerStyle}"
Background="{StaticResource DefaultBackground}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent">
<local:Forecast_SearchResults_ListView_Data/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl x:Class="SearchResultsBox">
我希望能够重用此列表框,并从外部上下文中添加一个新的datatemplate:
<local:SearchResultsBox>
<DataTemplate = {ForecastDataTemplate}/>
</local>
它会将此DataTemplate放入ListBox.ItemTemplate属性中。这可能吗?如果是,怎么做?如果没有,还有其他方法可以达到类似效果吗?您可以使用以下方法
<local:SearchResultsBox ItemTemplate="{StaticResource ForecastDataTemplate}" />
<ListBox x:Name="list" ... />
public DataTemplate ItemTemplate
{
get { return list.ItemTemplate;}
set { list.ItemTemplate = value;}
}