簡體   English   中英

如何設置綁定,以便一個控件中組合框的選定值更改另一控件的控件模板?

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

我有這樣的事情...第一個用戶控件如下

<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的組合框中選擇的任何內容

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

謝謝你的幫助!!!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM