小编Jon*_*eld的帖子

WPF - 如何获取绑定到ListBoxItem的对象

这就是我想做的事情.我从数据库中获取对象列表,并将此列表绑定到ListBox控件.ListBoxItems包含一个文本框和一个按钮.这就是我想出的.到目前为止,它按预期工作.该对象有许多属性,如ID,Name.如果我单击ListBoxItem中的按钮,则应该从ListBox和数据库中删除Item ...

<ListBox x:Name="taglistBox">    
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ListBoxItem">
                                            <ContentPresenter HorizontalAlignment="Stretch"/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Tag" Value="{Binding TagSelf}"></Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid HorizontalAlignment="Stretch">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Button Grid.Column="0" Name="btTag"  VerticalAlignment="Center"  Click="btTag_Click" HorizontalAlignment="Left">
                                        <Image Width="16" Height="16" Source="/WpfApplication1;component/Resources/104.png"/>
                                    </Button>
                                    <TextBlock Name="tbtagBoxTagItem" Margin="5" Grid.Column="1" Text="{Binding Name}" VerticalAlignment="Center" />                                        
                                 </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
Run Code Online (Sandbox Code Playgroud)

Textblock.Text绑定到object.Name,ListBoxItem.Tag绑定到object.TagSelf(它只是对象本身的副本).

现在我的问题

  1. 如果我单击listboxItem中的按钮,我如何获取listboxitem以及绑定到它的对象.为了从数据库中删除对象,我必须以某种方式检索它.我试过类似的东西

    ListBoxItem lbi1 =
    (ListBoxItem)(taglistBox.ItemContainerGenerator.ContainerFromItem(taglistBox.Items.CurrentItem)); ObjectInQuestion t =(ObjectInQuestion)lbi1.Tag;

  2. 如果Itemssource更改,有没有办法自动更新ListBox的内容?现在我正在实现这个目标

    taglistBox.ItemsSource = null;
    taglistBox.ItemsSource = ObjectInQuestion;

我很感激您能给予的任何帮助:D提前致谢

c# data-binding wpf listbox listboxitem

6
推荐指数
1
解决办法
2万
查看次数

函数包含另一个函数

我有一组静态函数

internal static List<ClassA> GetListOfClassA (int id, string name) {...}
internal static string GetName (int id) {...}
internal static List<ClassB> GetCompleteListOfClassB() {...}
Run Code Online (Sandbox Code Playgroud)

我想在这里完成的是以下(写为伪代码)

List<ClassA> newList = new List<ClassA>;
string newname;
List<ClassB> newListB = new List<ClassB>;

Pipe (newList = GetListOfClassA (5, "myname"));
Pipe (newname = GetName (5));
Pipe (newListB = GetCompleteListOfClassB());
Run Code Online (Sandbox Code Playgroud)

Pipe应该是一个函数,它接受带参数的另一个函数,执行一些代码并允许将函数的值返回给调用者.管道应该做类似的事情

{
 Console.WriteLine ("Test");
 if (CertainCondition==true)
     return GetName (value);
 else 
    wait(250);
}
Run Code Online (Sandbox Code Playgroud)

我尝试过各种各样的事情,比如lambda表达式或动作,但我不能让它一般地工作,以便接受所有类型的函数.

c# function

3
推荐指数
1
解决办法
55
查看次数

访问2010 VBA API TWIPS / PIXEL

有关API调用的问题以及适用于32位和64位系统的TWIPS /像素问题。我想要一个弹出窗口显示在鼠标指针的位置。我的解决方案工作正常(至少没有崩溃),但似乎无法计算出正确的位置。

'API Calls
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPtr

Private Declare PtrSafe Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As Long, lpRect As RECT_Type) As LongPtr

Private Declare PtrSafe Function apiGetDC Lib "user32" Alias "GetDC" (ByVal hWnd As Long) As LongPtr

Private Declare PtrSafe Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hDC As LongPtr, ByVal nIndex As Long) As LongPtr

Private Declare PtrSafe Function apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As …
Run Code Online (Sandbox Code Playgroud)

api ms-access vba pointers

1
推荐指数
1
解决办法
5055
查看次数

标签 统计

c# ×2

api ×1

data-binding ×1

function ×1

listbox ×1

listboxitem ×1

ms-access ×1

pointers ×1

vba ×1

wpf ×1