Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从外部设置用户控件的属性?_C#_Wpf_Xaml - Fatal编程技术网

C# 如何从外部设置用户控件的属性?

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

这是我的XAML:

<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;}
}