我有一个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