簡體   English   中英

XAML綁定用戶控件TextBlock不會顯示綁定數據

[英]XAML Bind a user controls TextBlock will not show binding data

我有一個列表視圖控件,它使用DataTemplate綁定數據。 在創建依賴項之后,綁定使用簡單的字符串,但在綁定類屬性時不起作用。 如果我將數據綁定到textBlock它可以工作,但如果我將相同的東西綁定到我的用戶控件它不起作用。

這是我的XAML:LISTVIEW

<ListView x:Name='lbUsers'
                 Height='370'
                 Canvas.Left='264'
                 Canvas.Top='183'
                 Width='1177'
                  Background='{x:Null}'>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <Views:UserSelectRibbon NameText ="{Binding Name}" />
                        <Image Width='10' />
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>

            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), 
            RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                               ItemWidth="{Binding (ListView.View).ItemWidth, 
            RelativeSource={RelativeSource AncestorType=ListView}}"
                               MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
                               ItemHeight="{Binding (ListView.View).ItemHeight, 
            RelativeSource={RelativeSource AncestorType=ListView}}" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

        </ListView>

這是我的用戶控制:

  public string NameText
        {
            get { return (string)GetValue(NameTextProperty); }
            set { SetValue(NameTextProperty, value); }
        }

        public static readonly DependencyProperty NameTextProperty =
            DependencyProperty.Register("NameText", typeof(string), typeof(UserSelectRibbon),null);

        public UserSelectRibbon()
        {
            InitializeComponent();
            DataContext = this;
        }

這是我簡單的用戶類:

public class User {
    public string Name { get; set; }
    public int Age { get; set; }
    public int Playlist { get; set; }
}

所以:在XAML中,如果我這樣做:工作

  <Views:UserSelectRibbon NameText ="Some text here" /> 

這將工作並將文本添加到用戶控件中的TextBlock

但是:在XAML中,如果我這樣做:不要“工作

<Views:UserSelectRibbon NameText ="{Binding Name}" />

我想知道為什么沒有綁定它可以工作,但它不適用於綁定

問題出在您的DataContext

public UserSelectRibbon()
{
    InitializeComponent();
    DataContext = this;
}

這對以下內容有影響:

<Views:UserSelectRibbon NameText ="{Binding Name}" />

在這里, DataContext已經更改為Views:UserSelectRibbon ,因此您無法再綁定到外部DataContext任何內容。

解決方案:不要從內部將UserControlDataContext設置為自身。 決不!

而是將其設置在usercontrols樹內的元素上,或使用一些RelativeSourceElementName綁定。

內部DataContext的解決方案:

<UserControl ...>
    <Grid x:Name="grid1">
        <TextBlock Text="{Binding NameText}"/>
    </Grid>
</UserControl>

public UserSelectRibbon()
{
    InitializeComponent();
    grid1.DataContext = this; // set DataContext on inner control instead of the usercontrol itself
}

使用RelativeSource的解決方案(您可以使用UserControl基類型或您的特定usercontrols內部類型):

<TextBlock Text="{Binding NameText,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>

ElementName解決方案:

<UserControl ...
             x:Name="myControl">
    <Grid>
        <TextBlock Text="{Binding NameText,ElementName=myControl}"/>
    </Grid>
</UserControl>

我最近用itemsControl做了類似的事情。 我可能會忘記一切。 無論如何,有一種更簡單的方法,然后從項目本身直接綁定。 相反,你從c#中調用該項目並在那里綁定。

Mainpage()
{
InitializeComponent();
}

仍然在括號中,你應該添加:

List<SampleItem> = new List<SampleItem>;
items.Add({NameText = Name});
lbusers.ItemsSource = items;

在括號外:

public class SampleItem{
public string Name {get; set;}
}

您可以根據需要添加items.Add行多次,這將顯示多少項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM