WPF 在 ListBox 中绑定 CustomControl 的 属性
WPF bind CustomControl's property in ListBox
我的 WPF 应用程序中有以下列表框:
<ListBox x:Name="Domaci_soupiska" Margin="0" Background="#FFF1DE8A" AllowDrop="True" Drop="Soupiska_D_drop" Grid.Column="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Typ, Mode=TwoWay}" />
<local:HracControl Hrac="{Binding Hrac, Mode=TwoWay}"/>
<TextBox Text="{Binding Cas, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
其中 ItemsSource
是 ObservableCollection<SoupiskaRow>
其中 SoupiskaRow
是一个简单的 class:
public class SoupiskaRow
{
public string Typ { get; set; }
public Hrac Hrac { get; set; }
public string Cas { get; set; }
}
并且 local:HracControl
是一个 UserControl
依赖 属性 Hrac
(几乎只是一个存储)。但是,当我将项目添加到 ObervableCollecion
时,只会在列表框内创建 "default" HracControl
- 没有任何数据集。此外,当我将断点放在 Hrac
dependency 属性 的设置部分时,数据绑定甚至 try 都没有设置这个 属性.
数据绑定可以很好地设置标签上下文或文本框文本,但我不明白为什么它甚至不尝试设置自定义 属性?
我发现了几个关于类似主题的话题,但他们建议将任何 CustomControls 和 copy/paste 其代码删除到 ListBox
的 DataTemplate
中。
HracControl代码比较简单:
public Hrac Hrac
{
get { return (Hrac)GetValue(HracProperty); }
set { SetValue(HracProperty, value);}
}
public static readonly DependencyProperty HracProperty =
DependencyProperty.Register("Hrac", typeof(Hrac), typeof(HracControl) , new PropertyMetadata(new Hrac()));
public HracControl()
{
this.InitializeComponent();
this.DataContext = this;
}
public HracControl(Hrac hrac)
{
this.InitializeComponent();
this.Hrac = hrac;
this.DataContext = this;
}
//and a methods for reordering/hiding some columns and drag/drop events
}
xaml内部:
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
...
</Grid.ColumnDefinitions>
<Label x:Name="Hrac_ID" Content="{Binding Hrac.Hrac_ID}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Label x:Name="Jmeno" Content="{Binding Hrac.Jmeno}" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="1"/>
...
</Grid>
如果您需要更多代码,请在评论中告诉我
如评论中所述,每个 FrameworkElement
只能有一个 DataContext
。它将通过可视化树继承或手动设置。
public HracControl(Hrac hrac)
{
...
this.DataContext = this;
}
通过这样做,您可以更改控件本身及其所有子控件的默认绑定上下文,包括
<local:HracControl Hrac="{Binding Hrac....}"/>.
因此它将在 HracControl
中搜索 Hrac
,因此它将尝试将 HracControl.Hrac
绑定到 HracControl.Hrac
。无需在构造函数中手动设置 DataContext
,您需要通过 ElementName
或 RelativeSource
绑定更改每个绑定的绑定上下文。
<UserControl ... x:Name="myControl">
<Grid ...>
<Label x:Name="Hrac_ID" Content="{Binding ElementName=myControl, Path=Hrac.Hrac_ID}" ... />
<Label x:Name="Jmeno" Content="{Binding ElementName=myControl, Path=Hrac.Jmeno}" .../>
</Grid>
</UserControl>
我的 WPF 应用程序中有以下列表框:
<ListBox x:Name="Domaci_soupiska" Margin="0" Background="#FFF1DE8A" AllowDrop="True" Drop="Soupiska_D_drop" Grid.Column="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Typ, Mode=TwoWay}" />
<local:HracControl Hrac="{Binding Hrac, Mode=TwoWay}"/>
<TextBox Text="{Binding Cas, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
其中 ItemsSource
是 ObservableCollection<SoupiskaRow>
其中 SoupiskaRow
是一个简单的 class:
public class SoupiskaRow
{
public string Typ { get; set; }
public Hrac Hrac { get; set; }
public string Cas { get; set; }
}
并且 local:HracControl
是一个 UserControl
依赖 属性 Hrac
(几乎只是一个存储)。但是,当我将项目添加到 ObervableCollecion
时,只会在列表框内创建 "default" HracControl
- 没有任何数据集。此外,当我将断点放在 Hrac
dependency 属性 的设置部分时,数据绑定甚至 try 都没有设置这个 属性.
数据绑定可以很好地设置标签上下文或文本框文本,但我不明白为什么它甚至不尝试设置自定义 属性?
我发现了几个关于类似主题的话题,但他们建议将任何 CustomControls 和 copy/paste 其代码删除到 ListBox
的 DataTemplate
中。
HracControl代码比较简单:
public Hrac Hrac
{
get { return (Hrac)GetValue(HracProperty); }
set { SetValue(HracProperty, value);}
}
public static readonly DependencyProperty HracProperty =
DependencyProperty.Register("Hrac", typeof(Hrac), typeof(HracControl) , new PropertyMetadata(new Hrac()));
public HracControl()
{
this.InitializeComponent();
this.DataContext = this;
}
public HracControl(Hrac hrac)
{
this.InitializeComponent();
this.Hrac = hrac;
this.DataContext = this;
}
//and a methods for reordering/hiding some columns and drag/drop events
}
xaml内部:
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
...
</Grid.ColumnDefinitions>
<Label x:Name="Hrac_ID" Content="{Binding Hrac.Hrac_ID}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Label x:Name="Jmeno" Content="{Binding Hrac.Jmeno}" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="1"/>
...
</Grid>
如果您需要更多代码,请在评论中告诉我
如评论中所述,每个 FrameworkElement
只能有一个 DataContext
。它将通过可视化树继承或手动设置。
public HracControl(Hrac hrac)
{
...
this.DataContext = this;
}
通过这样做,您可以更改控件本身及其所有子控件的默认绑定上下文,包括
<local:HracControl Hrac="{Binding Hrac....}"/>.
因此它将在 HracControl
中搜索 Hrac
,因此它将尝试将 HracControl.Hrac
绑定到 HracControl.Hrac
。无需在构造函数中手动设置 DataContext
,您需要通过 ElementName
或 RelativeSource
绑定更改每个绑定的绑定上下文。
<UserControl ... x:Name="myControl">
<Grid ...>
<Label x:Name="Hrac_ID" Content="{Binding ElementName=myControl, Path=Hrac.Hrac_ID}" ... />
<Label x:Name="Jmeno" Content="{Binding ElementName=myControl, Path=Hrac.Jmeno}" .../>
</Grid>
</UserControl>