简体   繁体   English

如何设置绑定,以便一个控件中组合框的选定值更改另一控件的控件模板?

[英]How do I setup binding so that selected value of combobox in one control changes control template of a different control?

I have something like this... The first user control is as below 我有这样的事情...第一个用户控件如下

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

The second user control is as below... please see where I have the uppercase comments in code. 第二个用户控件如下...请查看代码中有大写注释的位置。 I want to set the control template of PrintBox to whatever the user chooses in the combobox in the FirstControl 我想将PrintBox的控件模板设置为用户在FirstControl的组合框中选择的任何内容

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

Thanks for your help!!! 谢谢你的帮助!!!

  <Grid>
        <local:PrintBox x:Name="PrintBox"
                       Template="{Binding ElementName=PrintTemplateComboBox,Path=SelectedValue}"/>
  </Grid>

or you can use a converter to be certain what you return back 或者您可以使用转换器来确定返回的内容

    <UserControl.Resources>
        <Local:YourTemplateConverter x:Key="yourTemplateConverter"/>
    </UserControl.Resources>
    <Grid>
Template="{Binding ElementName=PrintTemplateComboBox,Path=SelectedValue,Converter={StaticResource yourTemplateConverter}}"
</Grid

and this is the converter 这是转换器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将绑定连接到(Winform)ComboBox 控件以从绑定源获取/设置控件中的选择? - How do I connect a binding to a (Winform) ComboBox control to get/set the selection in the control from the binding source? 绑定到组合框控件模板项失败 - Binding to the combobox control template item failed 当CellEditingTemplate DataTemplate控件更改其绑定值时,如何更新CellTemplate DataTemplate控件? - How do I update the CellTemplate DataTemplate control when the CellEditingTemplate DataTemplate control changes its bound value? 将控件值绑定到选定的datagrid值 - Binding a control value to selected datagrid value 如何从其他控件中清除asp:textbox控件? - How do I clear an asp:textbox control from a different control? 绑定到控件模板 - Binding to a Control Template 如何在WPF中访问控件模板内的组合框? - How to access combobox inside control template in WPF? Winforms ComboBox用户控件绑定 - Winforms ComboBox User Control Binding 如何在“模板/样式”中创建控件,以便可以对其进行访问? - How can I make a control in a Template/Style so that it can be accessed? 如何从ComboBox控件获取ToggleButton - How do I get the ToggleButton from ComboBox Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM