【问题标题】:Binding from style template从样式模板绑定
【发布时间】:2018-01-30 12:27:45
【问题描述】:

这就是我要完成的任务 - 我有一个 Item 控件,其项目源设置为 ObservableCollection 此集合中的每个项目都用作 viewModel 以在 ItemControl 中创建不同的按钮。我想知道如何从按钮样式模板绑定到此视图模型(PersonViewModel)的属性?可以说,我想使用 PersonViewModel 中定义的属性来控制自定义按钮中特定元素的可见性。这是一个小示例代码:

public class MainViewModel : ViewModelBase
{
    private ObservableCollection<PersonViewModel> _personViewModelList;
    public ObservableCollection<PersonViewModel> PersonViewModelList
    {
        get => _personViewModelList;
        set
        {
            _personViewModelList= value;
            OnPropertyChanged("PersonViewModelList");
        }
    }
}
public class PersonViewModel 
{
  private bool _visible;
  public bool Visible
    {
        get => _visible;
        set
        {
            _visible= value;
            OnPropertyChanged("Visible");
        }
    }
 }

这是我的项目控制:

<ItemsControl ItemsSource="{Binding PersonViewModelList}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Width="360" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button 
                    Style="{StaticResource ImageButton}">                       
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
</ItemsControl>

这是我的自定义按钮样式:

<Style x:Key="ImageButton" TargetType="Button">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="20" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20" />
                                <ColumnDefinition Width="20" />
                            </Grid.ColumnDefinitions>
//Here I want to bind to "Visible" property in PersonViewModel class. Any ideas on how to accomplish it?
                            <TextBlock Visibility="{Binding...}" Grid.Row="0" Grid.Column="1"  />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
    </Style>

【问题讨论】:

  • {Binding Visible, Converter={StaticResource BooleanToVisibilityConverter}} 应该可以工作,当然需要适当的转换器资源。
  • @Clemens 我已经尝试过了,它不起作用。我认为这种方式找不到“可见”属性。
  • @niks:应该。用 MCVE 证明你的观点:stackoverflow.com/help/mcve

标签: wpf data-binding


【解决方案1】:

你可以这样使用:

<TextBlock Visibility="{Binding DataContext.Visible, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=Button}}" Grid.Row="0" Grid.Column="1"  />

问题是你的 Button 的 ContentPresenter 有 DataContext = null。

【讨论】:

    猜你喜欢
    • 2016-01-14
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2015-07-03
    • 2011-06-04
    • 1970-01-01
    相关资源
    最近更新 更多