2016-04-04 9 views
1

名前空間の外で宣言されたさまざまな型を持つライブラリを使用する必要があります。 DataTriggerの一部としてこのように宣言された列挙型を使用しようとしていますが、参照する方法を考えることができません。ポイントを証明するには、次のViewModelにしてXAMLからグローバル名前空間の列挙型にアクセスする方法

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:l="clr-namespace:WpfApplication2" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <l:ViewModel /> 
    </Window.DataContext> 

    <Window.Resources> 
     <Style TargetType="{x:Type Button}"> 
      <Setter Property="Background" Value="LightBlue" /> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding EnumVal}" Value="{x:Static l:MyEnum.Val1}"> 
        <Setter Property="Background" Value="LightGreen" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

    <Grid> 
     <Button Content="Test"> 
     </Button> 
    </Grid> 
</Window> 

を(注、MyEnumは、すべての名前空間の外で宣言されている):このように

public enum MyEnum 
{ 
    Val1, 
    Val2 
} 

namespace WpfApplication2 
{ 
    public class ViewModel 
    { 
     public ViewModel() 
     { 
      EnumVal = MyEnum.Val1; 
     } 

     public MyEnum EnumVal { get; set; } 
    } 
} 

答えて

1

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:global="clr-namespace:" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="500" Width="500"> 
    <Button Content="{x:Static global:MyEnum.Val1}" /> 
</Window> 
+0

実は、これはこの中で動作しますたとえば、私の主なアプリケーションではありません。私の主なアプリでは、使用する必要のあるタイプが別のアセンブリにあります。私はそれをxmlns:global = "clr-namespace:; assembly = blah"のように参照する必要がありますが、これはうまくいきません。 – user3690202

関連する問題