如何在ControlTemplate中绑定ComboBox的ItemsSource?

时间:2018-05-29 11:15:20

标签: c# xaml uwp

情况

我必须在GridView中更改Flyout项的内容。所以我在ControlTemplate中创建Page.Resources并在ContentControl内设置Flyout

问题

ComboBox中有ControlTemplate。现在,我想将ItemsSource的{​​{1}}设置为ComboBox List<string>_easingType

问题

如何在MainPage中绑定/设置ItemsSource的{​​{1}}?

代码

我删除了代码中不必要的部分

XAML

ComboBox

代码背后

ControlTemplate

完整代码

AnimationSetSamplePage.zip

2 个答案:

答案 0 :(得分:2)

Flyout控件中的DataContext实际上是&#34; _items&#34;中的每个项目。您将要创建一个DataContext代理来访问您的Page的DataContext。您可以按照这两个链接中的任何一个来创建代理。

https://weblogs.asp.net/dwahlin/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls

http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

它的要点是您要创建一个可以作为静态资源引用的代理。在第一个链接之后,您将执行以下操作:

public class DataContextProxy : FrameworkElement
{
    public DataContextProxy()
    {
        this.Loaded += new RoutedEventHandler(DataContextProxy_Loaded);
    }

    void DataContextProxy_Loaded(object sender, RoutedEventArgs e)
    {
        Binding binding = new Binding();
        if (!String.IsNullOrEmpty(BindingPropertyName))
        {
            binding.Path = new PropertyPath(BindingPropertyName);
        }
        binding.Source = this.DataContext;
        binding.Mode = BindingMode;
        this.SetBinding(DataContextProxy.DataSourceProperty, binding);             
    }

    public Object DataSource
    {
        get { return (Object)GetValue(DataSourceProperty); }
        set { SetValue(DataSourceProperty, value); }
    }

    public static readonly DependencyProperty DataSourceProperty =
        DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null);


    public string BindingPropertyName { get; set; }

    public BindingMode BindingMode { get; set; }

}

您应该对public

使用_easingType访问修饰符
public List<string> _easingType = new List<string>(Enum.GetNames(typeof(EasingType)).ToArray());

在MainPage.xaml

<Page.Resources>        
    <local:DataContextProxy x:Key="DataContextProxy" />
    <ControlTemplate x:Key="BlurEditFlyout">
        ....
        <ComboBox ItemsSource="{Binding Source={StaticResource DataContextProxy}, Path=DataSource._easingType}" />
        ....
    <ControlTemplate x:Key="BlurEditFlyout">
</Page.Resources>
...

答案 1 :(得分:0)

  

如何在ControlTemplate中绑定/设置ComboBox的ItemsSource

我不确定您是否有充分理由提出此问题,但直接回答此问题,我们可以将字符串列表_esaingType设置为DataContext属性的值,绑定它。例如:

XAML

<Page.Resources>
    <ControlTemplate TargetType="FlyoutPresenter"  x:Key="BlurEditFlyout" >
      ...                   
                <ComboBox ItemsSource="{Binding}" />
      ...
    </ControlTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    <Button>
        <Button.Flyout>
            <Flyout>
                <Flyout.FlyoutPresenterStyle>
                    <Style TargetType="FlyoutPresenter">
                        <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
                        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
                        <Setter Property="Template" Value="{StaticResource BlurEditFlyout}">                                
                        </Setter>
                    </Style>
                </Flyout.FlyoutPresenterStyle>
                <!--<ContentControl Template="{StaticResource BlurEditFlyout}"/>-->
            </Flyout>
        </Button.Flyout>
        <SymbolIcon Symbol="Edit"/>
    </Button> 
</Grid>

背后的代码

List<string> _easingType = new List<string>();  
public MainPage()
{
   this.InitializeComponent();
   _easingType.Add("test2");
   _easingType.Add("test1");
   this.DataContext = _easingType;
}

对此方式的任何疑虑或使用此方面的任何问题请告诉我,我会及时跟进。更多详情请参阅Data binding in depth