【问题标题】:Windows Phone CustomControl Data BindWindows Phone 自定义控件数据绑定
【发布时间】:2015-02-23 23:17:40
【问题描述】:

它是 Windows Phone 8.1(运行时)

我在将自定义用户控件与数据列表绑定时遇到了一些问题。我会尽量简化。

我的问题是,如果我在自定义控件中使用 DataBind {Binding Something},它将无法正常工作。

我需要将绑定的数据(字符串)传输到自定义控件。

奇怪的是,如果我不使用DataBind,它会正常工作。例如 MyCustomControllParameter = "some string"(在我的示例中为 'BindingTextValue' 属性)

有谁知道如何将自定义用户控件与内部 ListView 与 DataTemplate 绑定。

假设:

XAML 测试主页

<Grid  Background="Black">
    <ListView x:Name="TestList" Background="#FFEAEAEA">

        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Background="#FF727272">
                    <local:TextBoxS BindingTextValue="{Binding Tag, FallbackValue='aSource'}" local:TextBoxS>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>



    </ListView>

</Grid>

XAML Test-Main page c#

public sealed partial class MainPage : Page
{
    List<TTag> tags = new List<TTag>();

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }


    public class TTag
    {
        public string Tag { get; set; }
    }

    private void InitializeAppData()
    {
        TTag tag = new TTag() { Tag = "hello world" };
        tags.Add(tag);
        tags.Add(tag);
        tags.Add(tag);
        TestList.ItemsSource = tags;
    }

         protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        InitializeAppData();
    }


}

用户控制 XAML:

<UserControl
x:Class="CustomControllTest.TextBoxS"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CustomControllTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="#FF4F4F4F"   >
    <RichTextBlock x:Name="MyTestBlock">
    </RichTextBlock>
</Grid>

用户控制 c# .cs

    public TextBoxS()
    {
        this.InitializeComponent();
        LayoutRoot.DataContext = this;

    }


    public static readonly DependencyProperty BindingTextValueProperty = DependencyProperty.Register(
                                     "BindingTextValue",
                                     typeof(string),
                                     typeof(TextBoxS),
                                     new PropertyMetadata(default(string)));

    public string BindingTextValue
    {
        get
        {
            return GetValue(BindingTextValueProperty) as string;
        }
        set
        {
            SetValue(BindingTextValueProperty, value);
            //This method adds some custom logic into RichTextBlock, pointed correctly
            SetupSpotterBox(value);
        }
    }

感谢您的帮助;)

【问题讨论】:

    标签: c# xaml data-binding windows-phone


    【解决方案1】:

    你需要设置 MainPage 的 DataContext 因为它没有设置在任何地方

    public MainPage()
    {
        this.InitializeComponent();
    
        this.NavigationCacheMode = NavigationCacheMode.Required;
        this.DataContext = this;
    }
    

    下一步删除您的用户控件中的以下行

    this.DataContext = this;
    

    这是为了让您的用户控件选择正确的 DataContext,例如 MainPages

    最后对您的 UserControl xaml 进行一些更改

    <UserControl
    x:Class="App21.TextBoxS"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App21"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400" x:Name="root">
    
    <Grid x:Name="LayoutRoot">
        <TextBlock x:Name="MyTestBlock" FontSize="22" 
                   Text="{Binding ElementName=root, Path=BindingTextValue}" Foreground="Red">
        </TextBlock>                    
    </Grid>
    

    请注意控件本身的 x:Name="root",我使用了 TextBlock,以便可以使用 ElementName 和 Dependency Property BondingTextValue 显示绑定到文本属性。

    【讨论】:

    • 没有问题不要忘记标记为答案,如果它解决了问题,如果它没有解决;)
    【解决方案2】:

    您不必在属性 Setter 中更新您的控件。试试这个代码:

    public static readonly DependencyProperty BindingTextValueProperty = DependencyProperty.Register(
                                     "BindingTextValue",
                                     typeof(string),
                                     typeof(TextBoxS),
                                     new PropertyMetadata(default(string), PropertyChangedCallback));
    
    
    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) {
    
        //This method adds some custom logic into RichTextBlock, pointed correctly
        var textBoxS = dependencyObject as TextBoxS;
    
        textBoxS.SetupSpotterBox((string) args.NewValue);
    }
    
    
    public string BindingTextValue
    {
        get { return GetValue(BindingTextValueProperty) as string; }
        set { SetValue(BindingTextValueProperty, value); }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 2011-03-21
      • 1970-01-01
      相关资源
      最近更新 更多