小编mjk*_*026的帖子

WPF:非空字符串时的触发条件

当然,我们可以像下面的代码一样检查一些控件的字符串属性是否为空:

<Trigger SourceName="atCaption" Property="Text" Value="{x:Static sys:String.Empty}">
    <Setter TargetName="imgBack" Property="Margin" Value="0"/>
    <Setter TargetName="atCaption" Property="Margin" Value="0"/>
</Trigger>
Run Code Online (Sandbox Code Playgroud)

然后,如何定义一个"非空"字符串的条件?

<!--unfortunately, can't accept '!=' operator in xaml.-->
<Trigger SourceName="atCaption" Property="Text" Value!="{x:Static sys:String.Empty}">
    <Setter TargetName="imgBack" Property="Margin" Value="0"/>
    <Setter TargetName="atCaption" Property="Margin" Value="0"/>
</Trigger>
Run Code Online (Sandbox Code Playgroud)

帮我.

c# wpf xaml triggers

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

在WPF中序列化数据绑定的ObservableCollection(PropertyChangedEventManager)

我试图通过数据绑定向Listbox显示一个列表.这是我的代码.

[Serializable]
public class RecordItem : INotifyPropertyChanged
{
    //implements of INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } }
}


[Serializable]
public class Records : ObservableCollection<RecordItem>
{
    public UOCRecords() { }

    public void Serialize(string path)
    {
        BinaryFormatter binForm = new BinaryFormatter();
        using (FileStream sw = File.Create(path))
        {
            binForm.Serialize(sw, this);
            sw.Close();
        }
    }

    public static UOCRecords Deserialize(string path)
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上它工作得很好,但是当我使用数据绑定时

this.lbData.ItemsSource = myRecents;
Run Code Online (Sandbox Code Playgroud)

并尝试执行序列化

this.myRecents.Serialize(recentsPath);
Run Code Online (Sandbox Code Playgroud)

它失败并出现此错误:

在Assembly'WpfApplication1,Version …

wpf serialization

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

关于枚举更优雅的设计

我正在学习C#.

我听说C#是最具建设性的语言之一.那么你们会让我的代码更加优雅和高效吗?

public class ISO639
{
    public enum ISO639Code
    {
        Afrikaans,                      //af
        Albanian,                       //sq
        Amharic,                        //am

        ...

        Yiddish,                        //yi
        Unknown                         
    }


    public static string GetISO639CodeString(ISO639.ISO639Code l)
    {
        switch (l)
        {
            case ISO639Code.English: return "en";
            case ISO639Code.Japanese: return "ja";

            ...

            case ISO639Code.Hebrew: return "he";
            default: return "";
        }


    public static ISO639.ISO639Code GetISO39CodeValue(string s)
    {

        switch (s)
        {
            case "ko" : return ISO639Code.Korean;
            case "en" : return ISO639Code.English;

            ...

            case "hu" : return ISO639Code.Hungarian;
            default: return ISO639Code.Unknown;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的班级ISO639.此类为ISO639代码提供枚举,但我需要从ISO639枚举到纯字符串的类型转换.(例如ISO639.ISO639Code.Italian =>"it").我还需要从普通字符串到ISO639枚举的类型转换.(例如"it"=> …

c# enums

7
推荐指数
2
解决办法
469
查看次数

WPF:在某个控件的Trigger中追逐另一个控件的属性

这是我的文本块.

    <Image x:Name:imgAnother/>

    <TextBlock>
        this is my text block
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="TextDecorations" Value="None"/>
                <Style.Triggers>
                    <Trigger Property="TextBlock.IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="RoyalBlue"/>
                        <!--I like to insert a code at here that changes another control's property...-->
                    </Trigger>
                    <Trigger Property="TextBlock.IsMouseOver" Value="False">
                        <Setter Property="Foreground" Value="#FF808080"/>
                        <!--..and this line too.-->
                   </Trigger>
                </Style.Triggers>                    
            </Style>
        </TextBlock.Style>
    </TextBlock>
Run Code Online (Sandbox Code Playgroud)

我喜欢制作一个xaml代码,它可以改变另一个控件的功能,比如"imgAnother".

我怎样才能做到这一点?

wpf xaml triggers styles

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

WPF:在后台线程中加载UI是不可能的?

我正在制作一个构建一个巨大的FlowDocument的应用程序.构建FlowDocument所用的时间约为3~4秒.

所以我喜欢在BackgroundWorker中构建FlowDocument,而不是UI线程.但BackgroundWorker无法返回WPF UI对象.(它发生了InvalidOperationException异常.)

我怎么解决这个问题?

wpf multithreading backgroundworker

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

更多关于if语句的简短代码

我想尝试以下代码:

//all arrays are List<T> type.
if (m.terms[0] != null && m.terms[0].labels != null && m.terms[0].labels[0].title == "Part-of-speech")
{
    result = true; 
}
Run Code Online (Sandbox Code Playgroud)

但在以下情况下偶尔会发生运行时错误

一世.m.terms == null

II.m.terms!= null,但是m.terms [0]没有初始化.

III.m.terms!= null,并且m.terms [0]已存在,但m.terms [0] .label未初始化.

...

所以我修改它像这样:

if (m.terms[0] != null)
{
    if (m.terms[0].labels != null)
    {
        if (m.terms[0].labels[0].title == "Part-of-speech") { result = true; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是最好的方式吗?

c# if-statement

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

SortDescription和自动排序顺序刷新

当我修改了在列表框中绑定的项目的值时,我预计排序的顺序应该自动更改.

但事实并非如此.

我是否调用.SortDescriptions.Clear()方法并在这种情况下重新分配SortDescription?

.Refresh()不起作用.

EDITED

我绑定并设置这样的数据;

public Records myRecents;


....

//lbToday is a ListBox.
//ModifiedTime is a DateTime.
this.lbToday.ItemsSource = new ListCollectionView(myRecents);
this.lbToday.Items.SortDescriptions.Add(new SortDescription("ModifiedTime", ListSortDirection.Descending));
Run Code Online (Sandbox Code Playgroud)

当应用程序第一次启动时,它显示正确的结果.但是当我修改item的值(在这种情况下,'ModifiedTime'属性)时,视图不会改变.我重新启动了应用程序,它再次显示了正确的结果.

EDITED2

这是一个源代码Records.

public class Records : ObservableCollection<RecordItem>
{
    public Records() { }

}
Run Code Online (Sandbox Code Playgroud)

这是'RecordItem'的源代码

public class RecordItem : INotifyPropertyChanged
{

    string queryString; public string QueryString { get { return queryString; } set { queryString = value; Notify("QueryString"); } }

    DateTime modifiedTime; public DateTime ModifiedTime { get { return modifiedTime; } set { …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf view

5
推荐指数
2
解决办法
4241
查看次数

NonSerialized无法工作

我是一个类的序列化,但我不能排除我班上的一些字段.

[Serializable]
public class DicData
{

    private GDicJson DeserializedGDicJson = new GDicJson();
    public UOCDicData BuiltDicData;                        

    [NonSerialized]
    public string CacheName = "";                          


}
Run Code Online (Sandbox Code Playgroud)

在我的预期中,公共字段CacheName未包含在我的*.xml反序列化输出中,但它包含在.xml文件中.

这里是序列化rutine.

XmlSerializer myXml = new XmlSerializer(typeof(DicData), "test");
myXml.Serialize(myFile, this); //note:a serializing perform in method of himself.
Run Code Online (Sandbox Code Playgroud)

c# xml serialization

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

WPF:一次移动并调整窗口大小

在Win32 API中,函数SetWindowPos提供了一种简单的方法来立即移动和调整窗口大小.

但是,在WPF类Window中没有类似的方法SetWindowPos.所以我必须编写如下代码:

        this.Left += e.HorizontalChange;
        this.Top += e.VerticalChange;
        this.Width = newWidth;
        this.Height = newHeight;
Run Code Online (Sandbox Code Playgroud)

当然,它运作良好,但并不简单.它看起来很脏.

如何移动窗口并立即调整大小?

有API吗?

wpf resize window

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

Control.Focus()和FocusManager.SetFocusedElement()之间的区别

从字面上看,我想知道这一点.

在某些情况下,.Focus()看起来比SetFocusedElement()更好.但另一个案例,它是逆转.所以我必须知道那里有什么不同的东西.

此外,通过MSDN,Focus()用于键盘焦点,而SetFocusedElement用于逻辑焦点.但我在逻辑焦点和键盘焦点之间感觉不一样.

wpf focus

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