首页 文章

在xaml中为集合中的元素定义绑定不会起作用

提问于
浏览
1

我有一个简单的控件与元素集合 . 我想 add elements in xamlbind to the element .

但是,当我在xaml中绑定到Bar.Value时,它永远不会起作用 . 最小的例子:

[ContentProperty("Bars")]
public class FooControl : Control
{
    private ObservableCollection<IBar> _bars = new ObservableCollection<IBar>();
    public ObservableCollection<IBar> Bars { get { return _bars; } }
}

public interface IBar
{
    string Value { get; }
}

public class Bar : DependencyObject, IBar
{
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(Bar), new PropertyMetadata("<not set>"));
    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
}
<Window x:Class="WpfTestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:this="clr-namespace:WpfTestApplication"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="200" Width="1000">

    <Window.Resources>
        <Style TargetType="this:FooControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="this:FooControl">
                        <ItemsControl ItemsSource="{Binding Bars, RelativeSource={RelativeSource TemplatedParent}}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Value}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Window.DataContext>
        <sys:String>from DataContext</sys:String>
    </Window.DataContext>
    <Grid>
        <this:FooControl>
            <this:Bar Value="directly set"/>
            <this:Bar Value="{Binding Source=from_binding_source}"/>
            <this:Bar Value="{Binding}"/>
            <this:Bar Value="{Binding Text, ElementName=SomeTextBlock}"/>
        </this:FooControl>
        <TextBlock Text="from TextBox" Name="SomeTextBlock" Visibility="Collapsed"/>
    </Grid>
</Window>

产量

directly set
from_binding_source
"<not set>"
"<not set>"

调试输出

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement . BindingExpression:路径=文本;的DataItem = NULL;目标元素是'Bar'(HashCode = 26568931); target属性是'Value'(类型'String')

Any suggestion how to get it to work?

我目前的解决方法是在代码中定义绑定,但这是更多的代码,并且在查看xaml时,哪些绑定确实存在并不明显 . (请参阅我的解决方法代码的答案)

2 回答

  • 1

    不知道这是否有帮助但是

    [ContentProperty("Bars")]
    public class FooControl : Control
    {
       private ObservableCollection<object> _bars = new ObservableCollection<object>();
       public ObservableCollection<object> Bars { get { return _bars; } }
    }
    

    XAML

    <this:FooControl>
            <this:Bar Value="directly set"/>
            <this:Bar Value="{Binding Source=from_binding_source}"/>
            <TextBlock Text="{Binding}"/>
            <TextBlock Text="{Binding Text, ElementName=SomeTextBlock}"/>
        </this:FooControl>
    

    你得到了你想要的结果

    enter image description here

  • 0

    到目前为止我的解决方法

    <this:Bar x:Name="bar3" />
        <this:Bar x:Name="bar4" />
    
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var bar3 = (DependencyObject)FindName("bar3");
        BindingOperations.SetBinding(bar3, Bar.ValueProperty, new Binding("DataContext") { Source = this });
    
        var bar4 = (DependencyObject)FindName("bar4");
        BindingOperations.SetBinding(bar4, Bar.ValueProperty, new Binding("Text") { Source = FindName("SomeTextBlock") });
    }
    

相关问题