Listview项Tooltip绑定viewmodel变量

时间:2017-08-23 12:00:16

标签: c# wpf xaml mvvm

SelectableItemClass有一个名为SI_itemName的字符串和一个名为SI_selected的bool。

视图模型:

private String _testBind;
public String testBind
{
    get
    {
       return _testBind;
    }
    set
    {
        _testBind = value;
        RaisePropertyChanged("testBind");
    }
}

private ObservableCollection<SelectableItemClass> _SList;
public ObservableCollection<SelectableItemClass> SList
{
    get { return _SList; }
    set
    {
        _SList= value;
        RaisePropertyChanged("SList");
    }
}

private SelectableItemClass _SelectedSItem;
public SelectableItemClass SelectedSItem
{
    get { return _SelectedSItem; }
    set
    {
        _SelectedSItem = value;
        testBind = "TESTTTTTTT";
        RaisePropertyChanged("SelectedSItem");
    }
}

查看:

<ListView ItemsSource="{Binding SList}" 
          SelectedItem="{Binding Path=SelectedSItem, Mode=TwoWay}" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          Margin="4,4,0,0" MinWidth="188" Height="200">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel.ToolTip>
                <ToolTip>
                    <Label Content="{Binding testBind}" />
                </ToolTip>                                
            </StackPanel.ToolTip>
            <StackPanel>
                <CheckBox Name="aaaa" Content="{Binding SI_itemName}" 
                                      IsChecked="{Binding SI_selected}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

问题是我无法在视图中的testBind内获得ToolTip的值。 选择元素时,将调用viewmodel中testBind的设置函数,但会在ToolTip上查看空字符串。

我也试过了 RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}标签上的ToolTip但不起作用。这是~DataContext or ItemTemplate`?

的问题

修改

工具提示和标签对于它所使用的标签具有相同的亲属来源,对于工具提示它没有

<StackPanel>
    <StackPanel.ToolTip>
        <ToolTip Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.testBind}" />
    </StackPanel.ToolTip>
    <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.testBind}" />
    <CheckBox Name="aaaa" Content="{Binding SI_itemName}" IsChecked="{Binding SI_selected}"/>
</StackPanel>

2 个答案:

答案 0 :(得分:0)

问题是ToolTip不是可视树的一部分。这就是为什么你必须像这样设置DataContext

<ToolTip DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">

如果您只想显示文字,也可以使用它。

<ToolTip Content="{Binding testBind}" />

修改

您的问题是您要绑定父级的属性而不是所选项的属性。你必须这样做

<ToolTip Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.testBind}" />

答案 1 :(得分:0)

使用上面评论WPF Binding to Tooltip中的链接,可以在stackpanel中为tag属性创建一个可绑定对象。

将此标记与点表示法一起使用,以便将工具提示中的不同元素绑定到对象中的相对属性。

public class PreviewItemClass : ObservableObject
{
    private string _descr1;
    public string descr1
    {
        get
        {
            return _descr1;
        }
        set
        {
            _descr1= value;
            RaisePropertyChanged("descr1");
        }
    }
    private string _descr2;
    public string descr2
    {
        get { return _descr2; }
        set
        {
            _descr = value;
            RaisePropertyChanged("descr2");
        }
    }

}

视图模型:

private SelectableItemClass _SelectedSItem;
public SelectableItemClass SelectedSItem
{
    get { return _SelectedSItem; }
    set
    {
        if (previewItem == null)
            previewItem = new PreviewItemClass();

        previewItem.descr1 = "aaa";
        previewItem.descr2 = "bbb";
        RaisePropertyChanged("SelectedSItem");
    }
}

查看:

<StackPanel DataContext="{Binding}" Tag="{Binding Path=DataContext.previewItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}">
    <StackPanel.ToolTip>
        <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
            <StackPanel Orientation="Vertical">
                <Label Content="{Binding Path=Tag.descr1}" />
                <Label Content="{Binding Path=Tag.descr2}" />
            </StackPanel>
        </ToolTip>
    </StackPanel.ToolTip>
    <CheckBox Name="aaaa" Content="{Binding SI_itemName}" IsChecked="{Binding SI_selected}"/>
</StackPanel>