代码之家  ›  专栏  ›  技术社区  ›  yoav.str

将组合框选定值作为另一个控件wpf的dataTemplate的键

  •  0
  • yoav.str  · 技术社区  · 15 年前

    我有一个组合框:

       <Grid>
    
        <ComboBox Height="52" HorizontalAlignment="Left"
                  ItemsSource="{Binding templatesNames}"
                  SelectedValuePath="Type.FullName"         
                  Margin="169,43,0,0" Name="comboBox1" 
                  VerticalAlignment="Top" Width="148" />
    
        <Button Content="Button"
       ***Template="{Binding key = Converter={Binding SelectedItem.Value,ElementName=comboBox1}}"
                Height="56" HorizontalAlignment="Left" 
                Margin="191,204,0,0" Name="button1" 
                VerticalAlignment="Top" Width="80" />
    </Grid>
    

    问题的签名是 * 我有模板如何保存模板名称 我想把这些名字转换成按钮要绑定到的密钥(模板密钥)

    我该怎么做?... 转换器应该做什么?需要吗?我可以不用吗?

    编辑: 这就是我现在所做的:

        <ComboBox x:Name="ComboBox1"
                                  ItemsSource="{Binding collection}" 
                                   Margin="553,0,0,13" 
                                  SelectedValuePath="Type.FullName" SelectedIndex="1"
                                  FontFamily="Buxton Sketch"
                                  FontSize="20" HorizontalAlignment="Left" Width="231" Height="46"     VerticalAlignment="Bottom" />
    

    其中collection是public observateCollection collection{get;set;}

    按钮类似于@Meleak按钮,项目名称空间是clr namespace:dinamicGridLayout 我应该在converter Uri resourceLocater=new Uri(@“clr namespace:dinamicGridLayout;ResourceDictionary1.xaml”,System.UriKind.Relative);)中写入吗;?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Fredrik Hedblad    15 年前

    如果您将所有的控件模板放在资源字典中,您可以使用这样的模板转换器

    <ComboBox Height="52" HorizontalAlignment="Left" 
              ItemsSource="{Binding templatesNames}" 
              SelectedValuePath="Type.FullName"          
              Margin="169,43,0,0" Name="comboBox1"  
              VerticalAlignment="Top" Width="148" /> 
    
    <Button Content="Button" 
            Template="{Binding SelectedItem.Value,
                               ElementName=comboBox1,
                               Converter={StaticResource TemplateConverter}}"
            Height="56" HorizontalAlignment="Left"  
            Margin="191,204,0,0" Name="button1"  
            VerticalAlignment="Top" Width="80" />
    

    在转换器中,从资源字典加载ControlTemplate并返回它。

    public class TemplateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string resourceKey = value.ToString();
            Uri resourceLocater = new Uri("/YourNamespace;component/Dictionary1.xaml", System.UriKind.Relative);
            ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
            return resourceDictionary[resourceKey] as ControlTemplate;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    

    更新

    如果您的命名空间是dinamicGridLayout,那么转换器应该如下所示

    Uri resourceLocater = new Uri("/dinamicGridLayout;component/Dictionary1.xaml", System.UriKind.Relative);
    

    上载的小示例项目 here .