Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# 如何设置绑定以使一个控件中combobox的选定值更改另一个控件的控件模板?_C#_Wpf_Binding_Combobox - Fatal编程技术网

C# 如何设置绑定以使一个控件中combobox的选定值更改另一个控件的控件模板?

C# 如何设置绑定以使一个控件中combobox的选定值更改另一个控件的控件模板?,c#,wpf,binding,combobox,C#,Wpf,Binding,Combobox,我有这样的东西。。。 第一个用户控件如下所示 <UserControl x:Class="FirstControl"> <UserControl.Resources> <ResourceDictionary x:Key="PrintTemplates> <ControlTemplate x:Key="PrintTemplateA"> ..... &

我有这样的东西。。。 第一个用户控件如下所示

<UserControl x:Class="FirstControl">
    <UserControl.Resources>
        <ResourceDictionary x:Key="PrintTemplates>
            <ControlTemplate x:Key="PrintTemplateA">
               .....
            </ControlTemplate>
            <ControlTemplate x:Key="PrintTemplateB">
               .....
            </ControlTemplate>
        <ResourceDictionary/>
    </UserControl.Resources>
    <Grid>
        <ComboBox x:Name="PrintTemplateComboBox" 
                  ItemsSource="{StaticResource PrintTemplates}" />
    </Grid>
</UserControl>
第二个用户控件如下所示。。。请查看代码中大写注释的位置。我想将PrintBox的控件模板设置为用户在FirstControl的combobox中选择的任何内容

<UserControl x:Class="SecondControl">
    <Grid>
        <local:PrintBox x:Name="PrintBox"
                        Template={Binding SelectedValue, Source= I WANT THIS TO BE PrintTemplateComboBox IN THE FirstControl />
    </Grid>
</UserControl>

谢谢你的帮助

您尝试过ElemenetName=PrintTemplateComboBox Path=SelectedValue吗?谢谢,但这在我的情况下不起作用,因为这些控件不在同一个窗口/控件上。谢谢,但在我的情况下,这不起作用,因为这些控件不在同一个窗口/控件上。因此,在这种情况下,您需要一个同时了解这两个控件的控制器。然后,您可以将一个控件的选择更改事件注册到另一个控件,并从中设置viewmodel中的绑定属性
  <Grid>
        <local:PrintBox x:Name="PrintBox"
                       Template="{Binding ElementName=PrintTemplateComboBox,Path=SelectedValue}"/>
  </Grid>
    <UserControl.Resources>
        <Local:YourTemplateConverter x:Key="yourTemplateConverter"/>
    </UserControl.Resources>
    <Grid>
Template="{Binding ElementName=PrintTemplateComboBox,Path=SelectedValue,Converter={StaticResource yourTemplateConverter}}"
</Grid
class YourTemplateConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
       //value is whwat YourTemplateConverter get from the other combobox
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}