小编Zol*_*rna的帖子

wpf eventsetter处理程序绑定样式

我有一个风格,我希望将命令绑定到EventSetterHandlerRelativeSource.该命令位于viewModel中.

<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" 
                 Handler="{Binding TextBlockMouseLeftButtonDownCommand, 
                           RelativeSource={RelativeSource Self}}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

问题是我得到一个错误,因为这有什么问题(也许不可能以这么简单的方式做到这一点)

我之前搜索了很多,我发现了AttachedCommandBehaviour,但我认为它不适用于风格.

你能给出一些如何解决这个问题的提示吗?

更新13/10/2011

我在MVVM Light Toolkit EventToCommand示例程序中找到了这个:

        <Button Background="{Binding Brushes.Brush1}"
            Margin="10"
            Style="{StaticResource ButtonStyle}"
            Content="Simple Command"
            Grid.Row="1"
            ToolTipService.ToolTip="Click to activate command">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cmd:EventToCommand Command="{Binding SimpleCommand}" />
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseLeave">
                <cmd:EventToCommand Command="{Binding ResetCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
Run Code Online (Sandbox Code Playgroud)

但在这里,绑定不是风格.我怎么能把它放到EventToCommand按钮的样式?

c# wpf binding

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

C#STAThread COMException

我有一个外部组件(C++),我想从我的C#代码中调用它.

代码是这样的:

using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace dgTEST
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            ExtComponentCaller extCompCaller = new ExtComponentCaller();
            result = extCompCaller.Call(input);

            Thread t = new Thread(new ThreadStart(() =>
            {
                try
                {
                    result = extCompCaller.Call(input);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }));

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以问题是,在第一次调用它运行良好时,调用了外部组件,我得到了结果.

但是当我尝试在另一个线程中调用它时,我得到一个异常:System.InvalidCastException:无法转换类型为'System .__ ComObject'的COM对象.... 由于STAThread,我确定这个例外被抛出了.因为如果我从Main函数中删除[STAThread]属性,那么第一次调用外部组件时也会发生同样的情况,这很正常.

如何从其他线程调用此外部组件以摆脱此异常?

UPDATE -------------

现在发生了其他疯狂的事情 当我从Visual Studio使用F5启动程序时,问题也出现在第一次调用中,但是当我直接执行二进制.exe文件时,它正在工作(从另一个线程它不是:().如果我切换从Debug到Release的构建,从Visual Studio和F5开始,第一次调用再次工作.

为什么会这样?

感谢您提前帮助!

最诚挚的问候,佐利

.net c# com sta

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

WPF ICollectionView 筛选器重置

我有CollectionView一个源自ObservableCollection

private static ObservableCollection<CalculationViewModel> _calculations;

CalculationViewModelsCollection = (CollectionView)CollectionViewSource.GetDefaultView(_calculations);
Run Code Online (Sandbox Code Playgroud)

我的问题是,当过滤器的结果是什么都没有时,我想清除过滤器,并用其他条件重新过滤,但CollectionView总是空的。

我尝试通过以下方式重置过滤器:

CalculationViewModelsCollection.Filter = null;
CalculationViewModelsCollection.Refresh();
Run Code Online (Sandbox Code Playgroud)

CalculationViewModelsCollection.Filter = delegate(object p)
{
    return true;
};
Run Code Online (Sandbox Code Playgroud)

但它们都不起作用。

您能否提供一些如何重置过滤器的建议CollectionView

c# wpf collectionview

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

标签 统计

c# ×3

wpf ×2

.net ×1

binding ×1

collectionview ×1

com ×1

sta ×1