简体   繁体   English

将元素绑定到子对象XAML

[英]Bind element to a child object XAML

first the code : 首先是代码:

MyStyle.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 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 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 Info.cs

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

I would like to bind MyDict "First" and "Second" in MyTextGen by the MainPage, is it possible ? 我想通过MainPage在MyTextGen绑定MyDict“ First”和“ Second”,这可能吗?

MyStyle.xaml 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 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 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 MainWindow.xaml

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

MainWindow.xaml.cs 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM