小编Rys*_*gan的帖子

WPF样式中的继承反转

我有一个包含按钮的UserControl:

<Button Content="Button"/>
Run Code Online (Sandbox Code Playgroud)

和风格:

<Style TargetType="Button">
    <Setter Property="Background" Value="Blue"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

父窗口(或另一个UserControl)可以设置另一种更通用的样式:

<Style TargetType="Button">
    <Setter Property="Background" Value="Red"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

结果是(很明显)父按钮将具有更一般的样式(红色),而我的用户控件将具有更具体样式(蓝色)的按钮.


我想知道如何反转这种行为,以实现像我的自定义用户控件中设置默认样式,然后可以在父控件或窗口中覆盖,如果有必要?

关键是,默认样式首先在自定义用户控件中定义,并由其父级自动覆盖.这就是我称之为反转的方式.


解决方案的可能示例如下所示:

<Style TargetType="Button" StylePriority="Default">
    <Setter Property="Background" Value="Blue"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

StylePriority会表示,如果有该按钮定义没有其他的风格,那么默认的样式应适用于它.

c# wpf

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

在资源中按样式指定Grid列和行定义

有一个带有以下网格的UserControl:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
</Grid>
Run Code Online (Sandbox Code Playgroud)

现在我有一个窗口,我会写这样的东西:

<Window.Resources>
    <Style TargetType="Grid">
        <Setter Property="RowDefinitions">
            <Value>
                <RowDefinition Height="*"/>
                <RowDefinition/>
            </Value>
        </Setter>
    </Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

关键部分,不编译是我想要将高度从自动更改为*.如何以合法的方式做到这一点?

一般来说,我必须处理案件.1)第一行应拉伸,第二行应固定.2)反之亦然.也许与Grid不同的面板可能更相关?

wpf xaml

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

如何在WPF中将TargetType设置为this?

我有一个带有几个子控件的自定义UserControl(例如名为MyUserControl).我想UserControl.Resources为设计目的创建一组样式和依赖属性赋值(没有样式控件看起来像泥球).后来我会评论这些风格.

问题是,我不知道如何设置TargetType样式以指出UserControl正在开发的样式.

下面是一个示例:

<UserControl.Resources>
    <Style TargetType="this">
    </Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

wpf xaml

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

Task.IsCancelled不起作用

我有以下示例代码:

static class Program
{
    static void Main()
    {
        var cts = new CancellationTokenSource();

        var task = Task.Factory.StartNew(
            () =>
                {
                    try
                    {
                        Console.WriteLine("Task: Running");
                        Thread.Sleep(5000);
                        Console.WriteLine("Task: ThrowIfCancellationRequested");
                        cts.Token.ThrowIfCancellationRequested();
                        Thread.Sleep(2000);
                        Console.WriteLine("Task: Completed");
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine("Task: " + exception.GetType().Name);
                        throw;
                    }
                }).ContinueWith(t => Console.WriteLine("ContinueWith: cts.IsCancellationRequested = {0}, task.IsCanceled = {1}, task.Exception = {2}", cts.IsCancellationRequested, t.IsCanceled, t.Exception == null ? "null" : t.Exception.GetType().Name));

        Thread.Sleep(1000);

        Console.WriteLine("Main: Cancel");
        cts.Cancel();

        try
        {
            Console.WriteLine("Main: Wait");
            task.Wait();
        }
        catch (Exception exception)
        {
            Console.WriteLine("Main: Catch …
Run Code Online (Sandbox Code Playgroud)

c# task task-parallel-library

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

如何设置Observable的处理动作?

我知道如何创建一个observable并分配一个处理动作:

Observable.Create(o =>
{
    // o.OnNext etc.
    return Disposable.Create(() => { /* ... */ });
});
Run Code Online (Sandbox Code Playgroud)

但是现在我从查询语法中产生了一个observable:

var observable = from x in otherObservable
                 select x;
Run Code Online (Sandbox Code Playgroud)

如何为此类查询分配处理操作?

c# system.reactive observable

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