我有一个RadioButton
元素,它的IsChecked
属性绑定到MyProperty
在ViewModel
.由于某些原因而Binding
具有模式OneWayToSource
,它将值RadioButton.IsChecked
推向ViewModel.MyProperty
.
RadioButton.IsChecked
最初false
,现在.我想设置一个初始值ViewModel
,可能是偶数true
.我不能这样做,因为属性被绑定占用.
有没有办法使用Binding
该模式并将默认值设置为UI中的绑定属性?像这样的东西:
<RadioButton IsChecked="{Binding MyProperty, Mode=OneWayToSource, DefaultVaule=???}">
</RadioButton>
Run Code Online (Sandbox Code Playgroud) 我有DataGrid
,其中一个DataGrid
列看起来像这样
<DataGridTextColumn Header="Value"
Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}"
x:Name="_col2"
IsReadOnly="True"
CanUserSort="False"
Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)
问题是我被迫使用BooleanToYesNoConverter
转换器两次.这意味着Convert
的方法BooleanToYesNoConverter
将被调用两次.因此,我想优化我的代码.并希望将ToolTip
属性值直接绑定到单元格的值.
我尝试使用ElementName
-s方法.但我不知道我应该在绑定的Path属性中指定什么.
<DataGridTextColumn Header="Value"
Binding="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}"
x:Name="_col2"
IsReadOnly="True"
CanUserSort="False"
Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ToolTip" Value="{Binding ElementName=_col2, Path=???}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)
我尝试使用DataGridTemplateColumn
而不是DataGridTextColumn
,但它也不起作用.
<DataGridTemplateColumn CanUserSort="False"
Header="????????"
Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value, Converter={StaticResource BooleanToYesNoConverter}}" …
Run Code Online (Sandbox Code Playgroud) 我这里有一些奇怪的问题.任何抛出的异常总是不能独立处理我如何尝试处理它.
我试试这个:
http://msdn.microsoft.com/en-us/library/dd997415%28v=vs.110%29.aspx
private class MyCustomException : Exception
{
public MyCustomException(String message) : base(message)
{
}
}
public static void Main()
{
var task1 = Task.Factory.StartNew(() =>
{
throw new MyCustomException("I'm bad, but not too bad!");
});
try
{
task1.Wait();
}
catch (AggregateException ae)
{
// Assume we know what's going on with this particular exception.
// Rethrow anything else. AggregateException.Handle provides
// another way to express this. See later example.
foreach (var e in ae.InnerExceptions)
{
if (e is …
Run Code Online (Sandbox Code Playgroud) 我有非常简单的布局,看起来:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="200"></ColumnDefinition>
<ColumnDefinition Width="5"></ColumnDefinition>
<ColumnDefinition Width="Auto" MinWidth="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0"
BorderBrush="Red"
BorderThickness="2">
<!-- Any picture-->
<Image Source="/Resources/PreviewTest.png"></Image>
</Border>
<GridSplitter Grid.Column="1"
Width="5"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
ResizeBehavior="PreviousAndNext"></GridSplitter>
<Expander Grid.Column="2"
ExpandDirection="Left"
BorderBrush="RoyalBlue"
BorderThickness="2">
<!-- Any picture-->
<Image Source="/Resources/PreviewTest.png"></Image>
</Expander>
</Grid>
Run Code Online (Sandbox Code Playgroud)
问题是:当我GridSplitter
向左拖动时,右栏从窗口边框出来,如动画所示.我发现当第三列的宽度设置为"Auto"(Width="Auto"
)时会发生这种情况.如果我设置Width="*"
GridSplitter
工作正常,第三个Column
不从窗口边框出去.那么为什么Width="Auto"
会发生呢?
我已经使用了Task
类型.一切都很好而Task
没有任何回报.例如:
XAML:
<Button Name="_button"
Click="ButtonBase_OnClick">
Click
</Button>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
_button.IsEnabled = false;
Task.Factory.StartNew(() =>
{
Thread.Sleep(5*1000);
Dispatcher.Invoke(new Action(() => _button.IsEnabled = true));
});
}
Run Code Online (Sandbox Code Playgroud)
这很好用.但是我想要Task
返回一些值,例如Boolean
.所以我需要使用Task<Boolean>
:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
_button.IsEnabled = false;
var task = Task<Boolean>.Factory.StartNew(() =>
{
Thread.Sleep(5*1000);
return true;
});
if (task.Result)
_button.IsEnabled = true;
}
Run Code Online (Sandbox Code Playgroud)
这里我们遇到了UI阻塞问题.UI线程被锁定,直到任务将返回结果.
_button.IsEnabled = false;
Run Code Online (Sandbox Code Playgroud)
因此,上面的字符串完全被忽略.我在.Net 4.0
,所以我不能使用async/await
方法.这个问题真的让我感到恶心......有解决方案吗?
我有一个简单Window
的按钮,第二个Window
是我点击时打开的Button
.第二个Window
有一个Image
控件,显示一个.png文件.因此,如果我使用FileObject
属性Binding
就可以了,我可以从中删除文件File Explorer
.但是,如果我使用FileName
属性,Binding
我无法删除文件File Explorer
,我得到操作系统异常.即使我GC
明确调用,即使关闭第二个窗口,我也无法做到这一点.FileName
财产有什么问题?有任何想法吗?
赢7,净4.0
窗口1
<Grid>
<Button Content="Ok"
Width="100"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Click="Click"
Padding="0,2,0,2"
IsDefault="True"
Name="_btnOk"/>
</Grid>
public partial class Window : Window
{
public Window()
{
InitializeComponent();
DataContext = this;
}
private void Click(Object sender, RoutedEventArgs e)
{
var window = new Window3();
window.ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud)
窗口2
<Grid>
<Image Source="{Binding FileObject}"></Image>
</Grid>
public partial …
Run Code Online (Sandbox Code Playgroud) 我有一个共同的任务。一键实现DataGrid中的CheckBox勾选。我决定创建一个从 DataGrid 派生的 DataGridExtended 类,并实现类似的东西:
XAML:
<DataGrid x:Class="DataGrid.DataGridExtended"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
代码:
public partial class DataGridExtended : System.Windows.Controls.DataGrid
{
private int _oldRowIndex;
private int _oldColumnIndex;
public DataGridExtended()
{
MouseLeftButtonUp += DataGridExtendedMouseLeftButtonUp;
MouseLeftButtonDown += DataGridExtendedMouseLeftButtonDown;
}
private void DataGridExtendedMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// ???? ?????? ??????? DataGridExtended
var dataGridExt = sender as DataGridExtended;
if (dataGridExt == null)
return;
// ???????? ??????? ??????
var currentCell = dataGridExt.CurrentCell;
_oldRowIndex = dataGridExt.SelectedIndex;
_oldColumnIndex = dataGridExt.CurrentColumn.DisplayIndex;
}
private void …
Run Code Online (Sandbox Code Playgroud) 我这里有一个非常简单的场景.请看布局,请:
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="0"></TextBox>
<DatePicker Grid.Row="1"
Name="_datePicker"
LostFocus="_datePicker_OnLostFocus"></DatePicker>
</Grid>
Run Code Online (Sandbox Code Playgroud)
和代码隐藏:
private void _datePicker_OnLostFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine("LostFocuse");
}
Run Code Online (Sandbox Code Playgroud)
所以,麻烦的是当我拿起一些日期然后点击时TextBox
,事件LostFocus
会触发7(七!)次.一个当DatePicker
我TextBox
完全失去焦点的时候,我完全停下来并且完全剩下6次对我来说是不可解释的.
我该如何解决?我只需要一次这次活动.或者我可以使用其他一些活动?我尝试LostKeyBoardFocus
了相同的结果.
我使用MVVM
模式开发应用程序.我使用MVVMLight库来做到这一点.所以如果我需要处理TextBox
TextChange
我在XAML中编写的事件:
<I:EventTrigger EventName="TextChanged">
<I:InvokeCommandAction Command="{Binding PropertyGridTextChange}"/>
</I:EventTrigger>
Run Code Online (Sandbox Code Playgroud)
哪里PropertyGridTextChange
是Command
在ViewModel
.但TextBox
没有Paste
事件!
此解决方案仅在应用程序不使用MVVM
模式时才有效,因为您需要链接TextBox
.
<DataTemplate x:Key="StringTemplate">
<TextBox Text="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
重要细节 - TextBox
置于其中DataTemplate
.我不知道如何处理"粘贴事件".我希望PasteCommand
在粘贴文本时调用TextBox
.而我需要TextBox.Text
或TextBox
自身被作为参数传递到PasteCommandMethod
.
private RelayCommand<Object> _pasteCommand;
public RelayCommand<Object> PasteCommand
{
get
{
return _pasteCommand ?? (_pasteCommand =
new RelayCommand<Object>(PasteCommandMethod));
}
}
private void PasteCommandMethod(Object obj)
{
}
Run Code Online (Sandbox Code Playgroud) wpf ×8
c# ×5
binding ×1
block ×1
column-width ×1
datagrid ×1
datagridcell ×1
datepicker ×1
exception ×1
gridsplitter ×1
image ×1
lost-focus ×1
mvvm ×1
oneway ×1
paste ×1
task ×1
textbox ×1
tooltip ×1