2015-03-23 4 views
0

первый код:Bind элемент дочернего объекта XAML

MyStyle.xaml

<Style x:Key="MyStyle" TargetType="Button"> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Grid> 
        <TextBlock x:Name="MyTextGen" Text="Foo"/> 
       </Grid> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

MyUserControl.xaml

<UserControl x:Name="Mycontrol"> 
    <Grid> 
     <Button x:Name="Btn1" Style="{StaticResource MyStyle}"/> 
     <Button x:Name="Btn2" Style="{StaticResource MyStyle}"/> 
    </Grid> 
</UserControl> 

MyPage.xaml

<common:MainPage x:Name="MainPage"> 
    <Grid> 
     <Popup x:Name="CatTool" IsOpen="False" HorizontalAlignment="Center" VerticalAlignment="Center" 
       Margin="0 0 300 500"> 
      <cat:MyControl > 
       <cat:MyContro.Transitions> 
        <TransitionCollection> 
         <PopupThemeTransition/> 
        </TransitionCollection> 
       </cat:MyContro.Transitions> 
      </cat:CatPagecontrol> 
     </Popup> 
    </Grid> 
</common:MainPage> 

Info.cs

MyDict = new Dictionary<string, string> 
{ 
    {"First", "MyFirst"}, 
    {"Second", "MySecond")} 
}; 

Я хотел бы связать MyDict "первый" и "второй" в MyTextGen по MainPage, возможно ли это?

+0

Хорошо, я не могу сказать "привет" -_- так жаль, что. –

ответ

0

MyStyle.xaml

<Style x:Key="MyStyle" TargetType="Button"> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Grid> 
        <TextBlock x:Name="MyTextGen" Text="{TemplateBinding Content}"/> 
       </Grid> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

MyUserControl.xaml

<UserControl> 
    <Grid> 
     <ItemsControl ItemsSource="{Binding Path=Buttons}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Style="{StaticResource MyStyle}" Content="{Binding}" Tag="{Binding}" /> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </Grid> 
</UserControl> 

MyUserControl.xaml.cs

public IEnumerable<string> Buttons { 
    get { return (IEnumerable<string>)GetValue(ButtonsProperty); } 
    set { SetValue(ButtonsProperty, value); } 
} 

public static readonly DependencyProperty ButtonsProperty = 
    DependencyProperty.Register("Buttons", typeof(IEnumerable<string>), 
    typeof(MyUserControl)); 

MainWindow.xaml

<Border> 
    <cat:MyUserControl Buttons="{Binding Buttons}" /> 
</Border> 

MainWindow.xaml.cs

public IEnumerable<string> Buttons { get; set; } 
private Dictionary<string, string> MyDict = new Dictionary<string, string> { { "First", "MyFirst" }, { "Second", "MySecond" } }; 

public MainWindow() { 
    this.DataContext = this; 
    Buttons = MyDict.Keys; 
    InitializeComponent(); 
} 
+0

Я меняю свой код, но эта работа прекрасна. –

Смежные вопросы