[英]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.