2010-11-16 59 views
0

为DataTemplate中的一个关键我有一个组合框:取下拉框选择的值作为另一个控制WPF

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

问题与签署* 我templatesNames如何保持我想隐蔽的模板 名这些名称转换为按钮即将绑定到的关键字(模板键)

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

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

<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是公共的ObservableCollection集合{获得;组; }

按钮是similer到@Meleak按钮,工程命名为CLR命名空间:dinamicGridLayout 我应该写转换器乌里resourceLocater =新的URI(@里面的 “CLR的命名空间:dinamicGridLayout; ResourceDictionary1.xaml”,系统.UriKind.Relative);?

回答

1

如果你把资源字典所有CONTROLTEMPLATES您可以使用转换为模板这样

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

而且在转换器,你从资源字典加载控件模板,并将其返回。

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

+0

这似乎是对的,但两个q: 1.StaticResource?你在做什么? 2./YourNamespace;component/Dictionary1.xaml“假设我的名字空间是xxxx,应该是”clr-namespace:xxx; Dictionary1.xaml“? – 2010-11-17 08:01:34

+0

请看我的编辑,谢谢... – 2010-11-17 08:17:55

+0

你是最棒的:) – 2010-11-17 12:49:53

相关问题